Skip to content

Instantly share code, notes, and snippets.

@petermichaux
petermichaux / adam.js
Last active December 10, 2015 12:08
An object as a function of it's messages.
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;
}
@petermichaux
petermichaux / View.js
Created July 1, 2012 17:12
View methods from Squeak ported to View methods in Maria
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;
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();
@petermichaux
petermichaux / gist:2593030
Created May 4, 2012 07:50
Golfing Maria Views
// 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
@petermichaux
petermichaux / gist:2552304
Created April 29, 2012 18:06
JavaScript's new and map don't play well together
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"
@petermichaux
petermichaux / gist:2489714
Created April 25, 2012 13:29
Function.prototype.new
Function.prototype.new = function() {
var obj = Object.create(this.prototype);
this.apply(obj, arguments);
return obj;
};
function Person(name) {
this.setName(name);
}
@petermichaux
petermichaux / gist:2360670
Created April 11, 2012 17:19
observer pattern interesting use cases
/*
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.
*/
/*
// 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, {
@petermichaux
petermichaux / gist:1969792
Created March 4, 2012 01:30
A faster Object.create polyfill that reuses F (doesn't support the second argument to Object.create)
if (typeof Object.create !== "function") {
Object.create = (function() {
function F() {}
return function (o) {
F.prototype = o;
return new F();
};
}());
var seq = seq || (function () {
// Private vars
var sequences = {},
def_seq = 'unnamed',
instance;
// Methods implementation
return {
curVal: function(seq_name) {