· 8 years ago · Jan 23, 2018, 02:52 AM
1var EventEmitter = require('events').EventEmitter,
2 uuid = require('node-uuid');
3
4var network = new EventEmitter();
5
6function User(network){
7 // The attributes we're interested in tracking
8 this.attributes = {
9 first_name: "",
10 last_name: "",
11 email: ""
12 };
13
14 // Uniquely identify the model
15 this.id = uuid();
16
17 // Subscribe to events about this particular model
18 network.on("user." + this.id, function(state){
19 // And our model is in sync
20 this.attributes = state;
21 }.bind(this));
22}
23
24var user = new User(network);
25
26network.emit("user.92329D39-6F5C-4520-ABFC-AAB64544E172", {
27 first_name: "Joshua",
28 last_name: "Moyers",
29 email: "jmoyers [at] gmail.com"
30});