Skip to content

Instantly share code, notes, and snippets.

@elplatt
Last active August 29, 2015 13:58
Show Gist options
  • Save elplatt/9940333 to your computer and use it in GitHub Desktop.
Save elplatt/9940333 to your computer and use it in GitHub Desktop.
Alternative to global namespacing. Rather than having globals or Singletons, declare each object you want to access to as a "lineage". The spawn() function is used to pass lineage values from an instance of class Elder to a new instance of class Younger: youngerInstance = elderInstance.spawn(Younger).
Pet = Model.extend({})
PetController = Controller.extend({
"initialize": function (options) {
this.names = options.names;
this.model = this.spawn(Pet)
this.view = this.spawn(PetView, {
"model": this.model
}) // "names" is passed implicitly
this.view.render();
}
})
PetView = View.extend({
"lineages": {"names": []}, // Set from spawning object if possible, otherwise default to []
"render": function () {
if (!this.model.get("name")) {
// "names" is never explicitly passed, but has been taken from PetController
return new Chooser(this.names))
}
return this.model.get("name")
}
})
c = new PetController({
"names": ['Lemon', 'Max', 'Cotton', 'Bruiser']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment