Skip to content

Instantly share code, notes, and snippets.

@darrenderidder
Created August 11, 2012 16:25
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darrenderidder/3325582 to your computer and use it in GitHub Desktop.
Save darrenderidder/3325582 to your computer and use it in GitHub Desktop.
simple mvc example
var Model = {};
var View = {};
var Controller = {};
var Model = {
counter: 0
};
var View = {
update: function(model) {
console.log(JSON.stringify(model));
}
};
var Controller = {
model: Model,
view: View,
count: function () {
this.model.counter++;
this.view.update(this.model);
}
};
Controller.count();
var Model = {
counter: 0
};
var View = {
update: function(model) {
console.log(JSON.stringify(model));
}
};
var Controller = {
model: Model,
view: View,
count: function () {
this.model.counter++;
this.view.update(this.model);
}
};
Controller.count();
Controller.count().count().count();
var Controller = {
model: Model,
view: View,
count: function () {
this.model.counter++;
this.view.update(this.model);
return this;
}
};
Controller.count().count().count();
var subscribers = [View];
function publish(info) {
for (i in subscribers) {
subscribers[i].update(info);
}
};
var Model = {
counter: 0,
incr: function () {
this.counter++;
publish(this);
}
};
var View = {
update: function(model) {
console.log(JSON.stringify(model));
}
};
var Controller = {
model: Model,
count: function () {
this.model.incr();
return this;
}
};
Controller.count().count().count();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment