this keyword
I am trying to understand the source code of the iot-geiger example. The source code file is under /both/collections.
this.Sensors = new Mongo.Collection("sensors");
this.Sensors.userCanInsert = function(userId, doc) {
return true;
};
this.Sensors.userCanUpdate = function(userId, doc) {
return true;
};
this.Sensors.userCanRemove = function(userId, doc) {
return true;
};
From what I understand "this" is the global object. Since this code snippet is run on both the client and the server, would two "Sensors" collections created on both ends? Are these two the same thing? Why not just put this code in the client or server folder?
@yilunyu this is how meteor works. Collection object is created both at the client and at the server. Client caches data from the server (minimongo) and client part of your application is using data from the client cache. When you insert data into collection, it is inserted into client cache (and UI behaves fast) and then client replicates that changes to the server in the background. After inserted record is written into server, server then updates all clients (their local cache) and data is visible to all other clients.
BTW This is out if scope of meteor kitchen, please read meteor documentation.
One more thing: entire process is transparent to developer. You job is to publish data at the server and subscribe at the client. All db updates are synchronised under the hood and UI automagically updates (it's reactive). That mechanism is Meteor's super power.
Ah, yes "this" is global object and can be omited. Reason why I put "this" is because js2coffee converter which converts this into @ symbol - that way code can be properly converted into coffe script (for people who still use it)