-
-
Save patrickandre/6985123 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * 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'] | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| * 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