Skip to content

Instantly share code, notes, and snippets.

Avatar

Edward L Platt elplatt

View GitHub Profile
@elplatt
elplatt / lineage-example.js
Last active Aug 29, 2015
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).
View lineage-example.js
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();
@elplatt
elplatt / dict-example.js
Created Aug 28, 2013
This JavaScript class wraps an Object to provide useful hash table / dictionary functionality. All data is maintained in the underlying Object. It doesn't pollute Object.prototype, so it plays nice with jQuery. See dict-example.js for examples.
View dict-example.js
map = {
"USA": { "Detroit": 23, "Cambridge": 5, "Cupertino": 0.5 },
"Canada": { "Waterloo": 1 },
"Switzerland": { "Geneva": 0.5 }
}
d = new Dict(map);
d.getDict("USA").get("Detroit"); // => 23
d.getDict("China", {}).get("Beijing", 0); // => 0
View index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<button id="button">Click me!</button>
<div id="new-color"></div>
<div id="all-colors"></div>
</body>