Skip to content

Instantly share code, notes, and snippets.

@pedrodelgallego
Created November 18, 2011 15:35
Show Gist options
  • Save pedrodelgallego/1376772 to your computer and use it in GitHub Desktop.
Save pedrodelgallego/1376772 to your computer and use it in GitHub Desktop.
describe("Model.extend", function() {
var applicant;
beforeEach(function(){
// I am passionate developer who learned to stop worrying and love Javascript
//
applicant = Person.init({
name: "Pedro Del Gallego",
github: "https://github.com/pedrodelgallego",
skills: ['ruby', 'css3', 'html5', 'scuba diving', 'js'],
passion: ["Javascript"]
})
})
it("The applicant should have a github account", function() {
expect(applicant.github).toBe("https://github.com/pedrodelgallego");
});
it("The applicant should have skills in js, css3, html5", function() {
expect(applicant.has_skillz("html5", "css3", "js")).toBe(true);
});
it("The applicant doesn't need skills in deer hunting", function() {
expect(applicant.has_skillz("deer hunting")).toBe(false);
});
});
(function( exports){
// Object.create() takes one argument, a prototype object, and returns
// a new object with the specified prototype object. In other words,
// you give it an object, and it returns a new one, inheriting from
// the one you specified.
// see http://javascript.crockford.com/prototypal.html
if (typeof Object.create !== "function"){
Object.create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
}
if (typeof Object.extend !== "function"){
Object.extend = function(destination, source) {
for (var property in source) {
if (typeof source[property] === "object" &&
source[property] !== null ) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};
};
var Model = {
inherited: function(){},
created: function(){},
prototype: { init: function(){} },
// The create() function returns a new object, inheriting from the
// Model object; we’ll use this for creating new models. The init()
// function returns a new object, inheriting from Model.prototype
create: function(){
var object = Object.create(this);
object.parent = this;
object.prototype = object.fn = Object.create(this.prototype);
object.created();
this.inherited(object);
return object;
},
init: function(){
var instance = Object.create(this.prototype);
instance.parent = this;
instance.init.apply(instance, arguments);
return instance;
},
extend: function(o){
var extend = Object.extend || $.extend;
var extended = o.extended;
extend(this, o);
if (extended) extended(this);
},
include: function(o){
var extend = Object.extend || $.extend;
var included = o.included;
extend(this.prototype, o);
if (included) included(this);
}
};
Model.include({
init: function(atts) {
if (atts) this.load(atts);
},
load: function(attributes){
for(var name in attributes)
this[name] = attributes[name];
}
})
exports.Model = Model
})(window)
Person = Model.create();
Person.include({
has_skillz: function(){
for (var i in arguments) {
return (this.skills.indexOf(arguments[i]) != -1);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment