Skip to content

Instantly share code, notes, and snippets.

@patrickandre
Forked from salami162/gist:5332074
Created October 15, 2013 01:20
Show Gist options
  • Save patrickandre/6985123 to your computer and use it in GitHub Desktop.
Save patrickandre/6985123 to your computer and use it in GitHub Desktop.
/**
* Implement Foo Object.
* - Take a JSON object as an input parameter
* - Create a copy this JSON object as itself.
* - Chainable
*/
var foo = new Foo({
version : '1.0'
});
// add components
foo
.set({
type : 'Foo',
container : '#foo'
})
// add features
.addItem(['item1','item3'])
.addItem('item2');
/**
* Expect output
*/
foo = {
version : '1.0',
type : 'Foo',
container : '#foo',
items : ['item1', 'item3', 'item2']
}
/**
* Foo Object
*/
var Foo = function (config) {
var options = JSON.parse( JSON.stringify(config) );
options.items = [];
_.extend(this, options);
};
Foo.prototype.set = function (config) {
_.extend(this, config);
return this;
};
Foo.prototype.addItem = function (items) {
items = _(items).isArray() ? items : [items];
this.items = _.union(this.items, items);
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment