Created
August 5, 2012 20:15
-
-
Save mkuklis/3266984 to your computer and use it in GitHub Desktop.
resource.js
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
var Resource = (function () { | |
// static API | |
var api = (function () { | |
this.models = []; | |
this.add = function (model) { | |
// TODO: make model a real model | |
this.models.push(model); | |
} | |
this.remove = function (model) { | |
// TODO: implement | |
} | |
this.get = function (model) { | |
// TODO: implement | |
} | |
this.all = function () { | |
return this.models; | |
} | |
this.at = function (index) { | |
return this.models[index]; | |
} | |
this.size = function () { | |
return this.model.length; | |
} | |
return this; | |
}); | |
// resource api | |
var remove = function () { | |
this.constructor.remove(this); | |
} | |
return function (options) { | |
var m = options.mutators || []; | |
// static api | |
api.call(this.constructor); | |
// mutators, accessors | |
m.forEach(function (name, index) { | |
var uname = name.charAt(0).toUpperCase() + name.slice(1); | |
this['set' + uname] = function (value) { | |
this[name] = value; | |
} | |
this['get' + uname] = function () { | |
return this[name]; | |
} | |
}, this); | |
// model api | |
this.remove = remove; | |
return this; | |
} | |
})(); | |
// usage: | |
function Car () {} | |
Resource.call(Car.prototype, {mutators: ['color', 'size']}); | |
Car.add({test: 1}); | |
var car = new Car(); | |
car.setColor('#ff0000'); | |
car.setSize(2); | |
car.getColor(); | |
car.getSize(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment