Skip to content

Instantly share code, notes, and snippets.

@patrickandre
Forked from salami162/gist:5332074
Created October 15, 2013 01:20
Show Gist options
  • Select an option

  • Save patrickandre/6985123 to your computer and use it in GitHub Desktop.

Select an option

Save patrickandre/6985123 to your computer and use it in GitHub Desktop.
/**
* Implement Firebird Object.
* - Take a JSON object as an input parameter
* - Create a copy this JSON object as itself.
* - Chainable
*/
var firebird = new Firebird({
version : '1.0'
});
// add components
firebird
.addComponent('list1', {
type : 'contentList',
container : '#container-123'
})
// add features
.addFeature('list', ['pagination','item'])
.addFeature('item', 'stars');
/**
* Expect output
*/
firebird = {
version : '1.0',
components : {
reviewList1 : {
id : 'list1',
type : 'contentList',
container : '#container-123',
features : {
list : ['pagination', 'item'],
item : ['stars']
}
}
}
}
/**
* Component Object
*/
var Component = function (config) {
this.components = _(config).isArray() ? config : [config];
}
Component.prototype.addFeature = function (owner, name) {
name = _(name).isArray() ? name : [name];
_(this.components).forEach(function (component) {
if ( !component.features ) {
component.features = {};
}
var path = component.features;
path[owner] = path[owner] || [];
_(name).forEach(function (newName) {
if ( !_(path[owner]).contains(newName) ) {
path[owner].push(newName);
}
});
});
return this;
};
/**
* Firebird Object
*/
var Firebird = function (config) {
var options = JSON.parse( JSON.stringify(config) );
options.components = {};
_.extend(this, options);
};
Firebird.prototype.addComponent = function (id, config) {
this.components[id] = _.extend({}, config, {
id : id
});
return new Component(this.components[id]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment