Skip to content

Instantly share code, notes, and snippets.

@jupiterjs
Created June 8, 2011 06:06
Show Gist options
  • Save jupiterjs/1013869 to your computer and use it in GitHub Desktop.
Save jupiterjs/1013869 to your computer and use it in GitHub Desktop.
Problems JavaScriptMVC Solves

Class

Tricky problems with prototypal inheritance.

Animal = function(){
  this.offspring = [];
};

Dog = function(){}
Dog.prototype = new Animal()

var dog1 = new Dog()
var dog2= new Dog();
dog1.offspring.push(dog2);
assertEqual(dog2.offspring[0], dog2);

The offspring array will be shared by all dogs. This is why dog2 has dog2 as offspring. $.Class fixes this:

$.Class('Animal',{
  init : function(){
    this.offspring = [];
  }
});

Animal('Dog')

Model

The observer pattern:

var model = new $.Model({val : 'bar'})
model.bind('val', function(){
  //called when val changes
})
model.attr('val', 'foo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment