Binding on Multiple custom kind instance

I have created a custom kind with a model. Something like this.
enyo.kind({<br />    name: "customViewModel",<br />    kind: "enyo.Model",<br />    attributes: {<br />        prop1: null,<br />        prop2: null<br />    }<br />});<br /><br />enyo.kind({<br />    name: "customView",<br />    model: new customViewModel(),<br />    components:[{name:"compA"}]<br />  <br />    bindings: [<br />                    { from: ".model.prop1", to: ".$.compA.content" }<br />                ]<br />(…)<br />})
But when I try to use this kind in a repeater, something wrong happens to the model data. All the views display the same info as the last kind object. It’s like there is the same instance view/model to all the repeated views.
This is how I have the repeater
<br />enyo.kind({<br />    name: "mainView",<br />    components: [<br />        {<br />            name: "repXPTO",<br />            kind: "enyo.DataRepeater",<br />            components: [{<br />                components: [{<br />                    kind: "customView", name: "cvItem"<br />                }],<br />                bindings: [<br />                    { from: ".model.prop1", to: ".$.cvItem.model.prop1" },<br />                    { from: ".model.prop1", to: ".$.cvItem.model.prop1" }<br />                ]<br />            }]<br />        }<br />    ],<br /><br />    startApp: function () {<br />                var collToBind = new Array();<br />                for (var i = 1; i < 6; i++) {<br /><br />                    collToBind.push(new customViewModel({ prop1: i, prop2: (i+i) }));<br />                }<br />                this.set(".$.repXPTO.collection", new enyo.Collection(collToBind));<br />    }<br /><br />});<br />
All de items in the repeater display de number 5, and not 0, 1, 2, 3, 4, 5 as expected.

Anny ideas?


  • Hmm, can you console.log out your collection, to see what's in the collection? And/or use transform on your bindings to see what values it's receiving?
  • I have put a log in the grid bind transform, and a observer (path: model.prop1) in the kind "customView"
    transformTest repXPTO VAL1 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o45 VAL1 0<br /><br />log.js:76 transformTest repXPTO VAL2 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o80 VAL2 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o45 VAL2 0<br /><br />log.js:76 transformTest repXPTO VAL3 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o157 VAL3 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o80 VAL3 0<br />log.js:76 transformTest - repXPTO - customView - observer[model.compA] euid:o45 VAL3 0<br />

    That is the result.
    It almost looks like after every item created by the repeater, when the bind occurs, its somehow connected to de previous models!
  • Okay. I know DataList uses a flyweight pattern but not sure if DataRepeater does. What may be happening is that DataRepeater is only creating one instance of the view and reusing it. can you try binding the model directly instead of the attributes of the model? Also, if you create jsfiddle, we can play around with it.
  • HI!
    I have tried what you said and nothing :neutral:

    If you notice, i'm printing the control ID (euid) and its always different.
    That makes me think that in fact is another instance of my view.

    What is the use of creating kinds if we are not able to use them multiple times?

    I am surely doing something wrong.

    Here is the fiddle

  • I changed it to bind model directly, rather than individual attributes and then it works:

    Prob something do with flyweight pattern to improve performance but I'd have to look at enyo source code to confirm/deny that.
  • I tried binding the whole model instead of the attributes and it seems that worked. In the customView kind, it is already binding the prop1 to the content.

    In the line 18:
    bindings: [ { from: ".model", to: ".$.cvItem.model" }]
    Is that what you expect?

    Hope it helps.
  • Hi. It worked. Thanks!

    So, for further reference, here is the working fiddler:

    By the way, is there any "mark as resolved" button in this forums?
  • edited April 2016
    Quick reminder: The leading dot on bindings is neither required nor recommended with Enyo 2.5+.
  • Also to note: All your custom views are sharing the same model instance because you are creating it once during the kind instantiation:

    model: new customViewModel(),

    You might want to create the model in the constructor if you want it to be unique for each custom view. Your binding in the DataRepeater does overwrite the custom view's model, of course.
Sign In or Register to comment.