This file contains hidden or 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
var adam = (function() { | |
var firstname = "Adam"; | |
var lastname = "of Eden"; | |
return function(message) { | |
switch (message) { | |
case "getName": | |
return firstname + " " + lastname; | |
default: | |
throw "unknown message " + message; | |
} |
This file contains hidden or 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
maria.View.prototype.getModel = function() { | |
return this._model; | |
}; | |
maria.View.prototype.setModel = function(model) { | |
this._setModelAndController(model, this._controller); | |
}; | |
maria.View.prototype.getDefaultControllerConstructor = function() { | |
return maria.Controller; |
This file contains hidden or 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
maria.SetModel.subclass(checkit, 'TodosModel', { | |
methods: { | |
getDone: function() { | |
return this.filter(function(todo) { | |
return todo.isDone(); | |
}); | |
}, | |
getUndone: function() { | |
return this.filter(function(todo) { | |
return !todo.isDone(); |
This file contains hidden or 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
// The following is sugar for writing out in full a view constructor function, its prototype, and the boilerplate | |
// for inheriting from maria.ElementView. This sugar uses naming conventions to wire together | |
// the view with its model, controller, and their methods. | |
// | |
// A checkit.TodoView will observe a checkit.TodoModel. When the model changes, the update method below is called. | |
// When a user clicks on the todo element, the handling is delegated to the checkit.TodoController's | |
// handleRootClick method. | |
// | |
maria.ElementView.declareConstructor(checkit, 'TodoView', { | |
template: '<li><span class="todo-content"></span></li>', // the template can live elsewhere, of course |
This file contains hidden or 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
var todoViews = todoModels.map(function(todoModel) { | |
return new TodoView(todoModel); | |
}); | |
// if Function.prototype.new is defined or TodoView.new is defined using "this" | |
var todoViews = todoModels.map(TodoView.new, TodoView); | |
// if Todo.new is defined without using "this" |
This file contains hidden or 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
Function.prototype.new = function() { | |
var obj = Object.create(this.prototype); | |
this.apply(obj, arguments); | |
return obj; | |
}; | |
function Person(name) { | |
this.setName(name); | |
} |
This file contains hidden or 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
/* | |
It's interesting to think about event/observer libraries for the first time after years of using | |
the same library, making messes, and trying to figure out how best to build reasonably large | |
browser apps. I realize that I want an event library that eases writing MVC applications in JavaScript. | |
*/ | |
/* |
This file contains hidden or 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
// Instead of this | |
var bar = Object.create(foo); | |
extend(bar, { | |
sayGoodbye: function() { alert("goodbye from " + this.name); } | |
}); | |
// why not write the following and provide a Object.create polyfill that supports the second argument? | |
var bar = Object.create(foo, { |
This file contains hidden or 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
if (typeof Object.create !== "function") { | |
Object.create = (function() { | |
function F() {} | |
return function (o) { | |
F.prototype = o; | |
return new F(); | |
}; | |
}()); |
This file contains hidden or 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
var seq = seq || (function () { | |
// Private vars | |
var sequences = {}, | |
def_seq = 'unnamed', | |
instance; | |
// Methods implementation | |
return { | |
curVal: function(seq_name) { |
NewerOlder