How to create a Collection which items are generated programmatically

How to create a collection with preinitialized values?

Comments

  • > var collection = new enyo.Collection([
    {name: "Ben Combee", handle: "unwiredben"},
    {name: "Cole Davis", handle: "clinuz"},
    {name: "Dave Freeman", handle: "sugardave"}
    ]);
    undefined
    > collection
    e {records: Array[3], length: 3, _recordChanged: function, _recordDestroyed: function, euid: "b0d27c89-15e6-252fdc63-3ebcaddf5016"…}
    > collection.at(0)
    e {owner: e, store: e, euid: "5a70dc69-5cf2-0858233e-84533b5942f4", attributes: Object, changed: Object…}
    > collection.at(0) instanceof enyo.Model
    true
    
  • Hi, unwiredben

    yes, I've read that a collection can parse data in it's arguments. But is there a way to define a collection in usual Enyo way using:

    enyo.kind({
    name: "Hello",
    kind: enyo.Collection..

    Thanks for your answer.

  • edited November 2013
    Probably something like:
    enyo.kind({
    name: "Hello",
    kind: "enyo.Collection",
    constructor: function(){
      this.inherited(arguments);
      this.add([
        {name: "Ben Combee", handle: "unwiredben"},
        {name: "Cole Davis", handle: "clinuz"},
        {name: "Dave Freeman", handle: "sugardave"}
      ]);
    }
    });
    
    Or, if you wanted to be evil, I imagine you could directly set "records" to your data array.
  • @mico, if you do that, you're actually defining a new kind based on enyo.Collection rather than an instance of enyo.Collection. That should be OK, but that's not how we've generally expected it to be used.
Sign In or Register to comment.