Last active
August 29, 2015 14:03
-
-
Save DmitryBaranovskiy/9a72314d5c801085fa56 to your computer and use it in GitHub Desktop.
Events…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?