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.

Revisions

  1. Limin Shen revised this gist Apr 8, 2013. 2 changed files with 24 additions and 53 deletions.
    32 changes: 12 additions & 20 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,29 @@
    /**
    * Implement Firebird Object.
    * Implement Foo Object.
    * - Take a JSON object as an input parameter
    * - Create a copy this JSON object as itself.
    * - Chainable
    */
    var firebird = new Firebird({
    var foo = new Foo({
    version : '1.0'
    });

    // add components
    firebird
    .addComponent('list1', {
    type : 'contentList',
    container : '#container-123'
    foo
    .set({
    type : 'Foo',
    container : '#foo'
    })
    // add features
    .addFeature('list', ['pagination','item'])
    .addFeature('item', 'stars');
    .addItem(['item1','item3'])
    .addItem('item2');

    /**
    * Expect output
    */
    firebird = {
    foo = {
    version : '1.0',
    components : {
    reviewList1 : {
    id : 'list1',
    type : 'contentList',
    container : '#container-123',
    features : {
    list : ['pagination', 'item'],
    item : ['stars']
    }
    }
    }
    type : 'Foo',
    container : '#foo',
    items : ['item1', 'item3', 'item2']
    }
    45 changes: 12 additions & 33 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,20 @@
    /**
    * Component Object
    * Foo 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 Foo = function (config) {
    var options = JSON.parse( JSON.stringify(config) );
    options.components = {};
    options.items = [];
    _.extend(this, options);
    };

    Firebird.prototype.addComponent = function (id, config) {
    this.components[id] = _.extend({}, config, {
    id : id
    });
    return new Component(this.components[id]);
    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;
    };

  2. Limin Shen revised this gist Apr 7, 2013. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,15 @@
    * Component Object
    */
    var Component = function (config) {
    this = config;
    this.components = _(config).isArray() ? config : [config];
    }

    Component.prototype.addFeature = function (owner, name) {
    name = _(name).isArray() ? name : [name];
    _(this).forEach(function (component) {
    _(this.components).forEach(function (component) {
    if ( !component.features ) {
    component.features = {};
    }
    var path = component.features;
    path[owner] = path[owner] || [];

    @@ -24,8 +27,9 @@ Component.prototype.addFeature = function (owner, name) {
    * Firebird Object
    */
    var Firebird = function (config) {
    this = JSON.parse( JSON.stringify(config) );
    this.components = {};
    var options = JSON.parse( JSON.stringify(config) );
    options.components = {};
    _.extend(this, options);
    };

    Firebird.prototype.addComponent = function (id, config) {
  3. Limin Shen revised this gist Apr 7, 2013. No changes.
  4. Limin Shen revised this gist Apr 7, 2013. 2 changed files with 43 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    /**
    * 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'
    });
    37 changes: 37 additions & 0 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /**
    * Component Object
    */
    var Component = function (config) {
    this = config;
    }

    Component.prototype.addFeature = function (owner, name) {
    name = _(name).isArray() ? name : [name];
    _(this).forEach(function (component) {
    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) {
    this = JSON.parse( JSON.stringify(config) );
    this.components = {};
    };

    Firebird.prototype.addComponent = function (id, config) {
    this.components[id] = _.extend({}, config, {
    id : id
    });
    return new Component(this.components[id]);
    };

  5. Limin Shen revised this gist Apr 7, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@ var firebird = new Firebird({

    // add components
    firebird
    .addComponent('reviewList1', {
    .addComponent('list1', {
    type : 'contentList',
    container : '#BVRRContainer'
    container : '#container-123'
    })
    // add features
    .addFeature('list', ['pagination','item'])
    @@ -19,9 +19,9 @@ firebird = {
    version : '1.0',
    components : {
    reviewList1 : {
    id : 'reviewList1',
    id : 'list1',
    type : 'contentList',
    container : '#BVRRContainer',
    container : '#container-123',
    features : {
    list : ['pagination', 'item'],
    item : ['stars']
  6. Limin Shen revised this gist Apr 7, 2013. No changes.
  7. Limin Shen renamed this gist Apr 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. Limin Shen revised this gist Apr 7, 2013. 2 changed files with 31 additions and 37 deletions.
    31 changes: 31 additions & 0 deletions Extend function
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    var firebird = new Firebird({
    version : '1.0'
    });

    // add components
    firebird
    .addComponent('reviewList1', {
    type : 'contentList',
    container : '#BVRRContainer'
    })
    // add features
    .addFeature('list', ['pagination','item'])
    .addFeature('item', 'stars');

    /**
    * Expect output
    */
    firebird = {
    version : '1.0',
    components : {
    reviewList1 : {
    id : 'reviewList1',
    type : 'contentList',
    container : '#BVRRContainer',
    features : {
    list : ['pagination', 'item'],
    item : ['stars']
    }
    }
    }
    }
    37 changes: 0 additions & 37 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,37 +0,0 @@
    var firebird = new Firebird(fbJson);

    // add components
    firebird
    .addComponent('reviewList1', {
    type : 'contentList',
    container : '#BVRRContainer'
    })
    // add features
    .addFeature('list', ['pagination','item'])
    .addFeature('item', 'stars');

    /**
    * Expect output
    *
    * firebird = {
    * components : {
    * reviewList1 : {
    * id : 'reviewList1',
    * type : 'contentList',
    * container : '#BVRRContainer',
    * features : {
    * list : ['pagination', 'item'],
    * item : ['stars']
    * }
    * }
    * }
    * }
    }
    }
    *
    }
    }
    * }
    *
    }
    */
  9. Limin Shen created this gist Apr 7, 2013.
    37 changes: 37 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    var firebird = new Firebird(fbJson);

    // add components
    firebird
    .addComponent('reviewList1', {
    type : 'contentList',
    container : '#BVRRContainer'
    })
    // add features
    .addFeature('list', ['pagination','item'])
    .addFeature('item', 'stars');

    /**
    * Expect output
    *
    * firebird = {
    * components : {
    * reviewList1 : {
    * id : 'reviewList1',
    * type : 'contentList',
    * container : '#BVRRContainer',
    * features : {
    * list : ['pagination', 'item'],
    * item : ['stars']
    * }
    * }
    * }
    * }
    }
    }
    *
    }
    }
    * }
    *
    }
    */