Skip to content

Instantly share code, notes, and snippets.

@wulymammoth
Last active January 15, 2019 21:20
Show Gist options
  • Select an option

  • Save wulymammoth/031367c6c62d1a60ca0f2ffe04580845 to your computer and use it in GitHub Desktop.

Select an option

Save wulymammoth/031367c6c62d1a60ca0f2ffe04580845 to your computer and use it in GitHub Desktop.
/* MVC */
// MODEL
function ModelBase(attributes) {
for (var attr in attributes) {
this[attr] = attributes[attr];
}
this._eventHandlers = {};
}
// TODO: change `attr` param to accept hash/object of attrs instead of
// a single attribute and set them like we do in the model constructor
ModelBase.prototype.set = function(attr, val) {
var changes = {};
if (this[attr] !== val) {
changes[change] = val;
}
if (Object.keys(changes).length) {
for (var change in changes) {
this[change] = changes[change];
}
this.trigger("change", changes);
}
}
ModelBase.prototype.get = function(attr) {
return this[attr];
}
ModelBase.prototype.on = function(event, eventHander) {
var evt = this._eventHandlers[event];
evt ? evt.push(eventHander) : evt = [eventHander];
}
ModelBase.prototype.trigger = function(event) {
var args = arguments;
this._eventHandlers[event].forEach(function(handler) {
handler(model, event, args);
});
}
// VIEW
function ViewBase(options) {
this.model = options.model;
this.el = options.el;
this.subViews = options.subViews || [];
this.model.on('change', this.render);
}
ViewBase.prototype.render = function() {
// update the view here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment