Skip to content

Instantly share code, notes, and snippets.

@yichaowang
Created September 7, 2012 05:50
Show Gist options
  • Save yichaowang/3663557 to your computer and use it in GitHub Desktop.
Save yichaowang/3663557 to your computer and use it in GitHub Desktop.
A simple ui collection to handle interaction between components
yam.ctor('yam.ui.collection', {
init: function() {
this._subscriptions = {};
this._handleCounter = 0;
}
, trigger: function (topic, args) {
yam.log('ui model trigged '+topic+' event');
var s = this._subscriptions
, res = [];
if (!s[topic]) { return; }
for (var id in s[topic]) {
var fn = s[topic][id];
if (args instanceof Array !== true) {
args = [args];
}
res.push(fn.apply(fn, args || []));
}
return res;
}
, bind: function(topic, fn) {
yam.log(fn + 'in model binded to '+topic+' event')
var self = this;
if (topic instanceof Array === true) {
var topics = [];
yam._.each(topic, function (x) {
topics.push(self.bind(x, fn));
});
return topics;
}
var id = this._handleCounter, s = this._subscriptions;
if (!s[topic]) { s[topic] = {}; }
s[topic][id] = fn;
this._handleCounter++;
return [topic, id];
}
, add: function() {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment