Last active
August 29, 2015 13:58
-
-
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).
This file contains 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
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