Skip to content

Instantly share code, notes, and snippets.

@DmitryBaranovskiy
Last active August 29, 2015 14:03
Show Gist options
  • Save DmitryBaranovskiy/9a72314d5c801085fa56 to your computer and use it in GitHub Desktop.
Save DmitryBaranovskiy/9a72314d5c801085fa56 to your computer and use it in GitHub Desktop.
Events…
// This gist is based on eve (https://github.com/adobe-webplatform/eve), but really any event based API will do
// Idea is to recreate backbone-like model, but on events only
// Model here is a hash-map, but it shouldn’t be. You could implement any structure this way.
function model(id) {
var modelid = ["model", id],
name = function () {
return modelid.concat.apply(modelid, arguments);
};
(function init() {
var model = {};
eve.on(name("set"), function (key, value) {
var old = {};
if (typeof key != "object") {
var attr = {};
attr[key] = value;
key = attr;
}
for (var k in key) {
old[k] = model[k];
model[k] = key[k];
eve(name("change", k), null, k, key[k], old[k]);
}
eve(name("update"), null, key, old);
});
eve.on(name("unset"), function (key) {
var old = model[key];
delete model[key];
eve(name("deleted"), null, key);
eve(name("update"));
});
eve.on(name("json"), function () {
this(JSON.stringify(model));
});
eve.on(name("get"), function (key) {
this(model[key]);
});
eve.on(name("has"), function (key) {
this(key in model);
});
eve.on(name("death"), function () {
eve.off(modelid);
});
}());
// the following part is optional.
// If id is known we could communicate with the model via events only
return {
id: function () {
return id;
},
"set": function (key, value) {
eve(name("set"), null, key, value);
},
"get": function (key) {
var res;
eve(name("get", key), function (value) {
res = value;
}, key);
return res;
},
has: function (key) {
var res;
eve(name("has"), function (value) {
res = value;
}, key);
return res;
},
unset: function (key) {
eve(name("unset"), null, key);
},
toJSON: function () {
var res;
eve(name("json"), function (value) {
res = value;
});
return res;
},
die: function () {
eve(name("death"));
},
// as I mentioned, I don’t like these methods, but for the sake of it…
listenTo: function (other, event, callback) {
var id = other.id(),
eventName = ["model", id].concat(event);
eve.on(eventName, callback);
eve.once(name("stopListening", id), function () {
eve.off(eventName, callback);
});
},
stopListening: function (other) {
eve(["stopListening", other ? other.id() : "*"]);
}
};
}
eve.on("*", function () {
console.debug(eve.nt());
});
var m1 = model(1),
m2 = model(2);
m1.set({a: 1, b: 2});
m1.die();
m2.set({a: m1.get("a"), b: m1.get("b")});
m2.unset("b");
console.log(m1.toJSON());
console.log(m2.toJSON());
@DmitryBaranovskiy
Copy link
Author

@hitsthings: Isn’t it the same as being afraid that somebody will overwrite Model.prototype.has?

@apsavin
Copy link

apsavin commented Jul 8, 2014

@DmitryBaranovskiy what do you want to do this? I don't get it, why do you need to recreate Backbone.Model api using events?

@apsavin
Copy link

apsavin commented Jul 8, 2014

The main prop is possibility to communicate with the model using events only (if id is known)?

@DmitryBaranovskiy
Copy link
Author

My original question was, “What do you think of API like this?”. Like 100% event based. To show the API I created purely hypothetical example. “Why?” is irrelevant. What pros and cons do you see?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment