Created
June 26, 2014 18:40
-
-
Save j0lvera/a8da346c1a45e8a5bfcb to your computer and use it in GitHub Desktop.
Collection class
This file contains hidden or 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
// It needs a lot more work | |
// | |
// http://singlepageappbook.com/collections3.html | |
// https://gist.github.com/g6scheme/4157554#controller-example-implementation | |
// http://alexatnet.com/articles/model-view-controller-mvc-javascript | |
var Collection = function(models, options) { | |
_.extend(this, options); | |
this.id = _.uniqueId('collection'); | |
var parts, selector, eventType; | |
if (this.events) { | |
_.each(this.events, function(method, eventName) { | |
parts = eventName.split(' '); | |
selector = parts[0]; | |
eventType = parts[1]; | |
$(selector)['on' + eventType] = this.[method]; | |
}.bind(this)); | |
} | |
}; | |
Collection.prototype.add = function(model, at) { | |
var self = this; | |
if (_.isArray(model)) { | |
return _.each(model, function(m) { | |
self.add(m, at); | |
}); | |
} | |
this._items.splice(at || this._items.length, 0, model); | |
this.trigger(this.id + "add", model); | |
}; | |
_.extend(Collection.prototype, Events); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment