0

我有一个数组。我想在 enyo.js 中给出数组的值。

我将如何动态填充数组?

以下是代码:

enyo.kind({
   name: "enyo.sample.RepeaterSample",
   classes: "enyo-fit repeater-sample",
    components: [
    {kind: "Repeater", onSetupItem:"setupItem", components: [
        {name:"item", classes:"repeater-sample-item", components: [
            {tag:"span", name: "personNumber"},
            {tag:"span", name: "personName"}
        ]}
    ]}
],
create: function() {
    this.inherited(arguments);
    this.$.repeater.setCount(this.people.length);
},
setupItem: function(inSender, inEvent) {
    var index = inEvent.index;
    var item = inEvent.item;
    var person = this.people[index];
    item.$.personNumber.setContent((index+1) + ". ");
    item.$.personName.setContent(person.name);
    item.$.personName.applyStyle("color", person.sex == "male" ? "dodgerblue" : "deeppink");
},
people: [
    {name: "Andrew", sex:"male"},
    {name: "Betty", sex:"female"},
    {name: "Christopher", sex:"male"},
    {name: "Donna", sex:"female"},
    {name: "Ephraim", sex:"male"},
    {name: "Frankie", sex:"male"},
    {name: "Gerald", sex:"male"},
    {name: "Heather", sex:"female"},
    {name: "Ingred", sex:"female"},
    {name: "Jack", sex:"male"},
    {name: "Kevin", sex:"male"},
    {name: "Lucy", sex:"female"},
    {name: "Matthew", sex:"male"},
    {name: "Noreen", sex:"female"},
    {name: "Oscar", sex:"male"},
    {name: "Pedro", sex:"male"},
    {name: "Quentin", sex:"male"},
    {name: "Ralph", sex:"male"},
    {name: "Steven", sex:"male"},
    {name: "Tracy", sex:"female"},
    {name: "Uma", sex:"female"},
    {name: "Victor", sex:"male"},
    {name: "Wendy", sex:"female"},
    {name: "Xin", sex:"male"},
    {name: "Yulia", sex:"female"},
    {name: "Zoltan"},
]

});

在这种情况下,人是硬编码的。我想动态地给出我从 json 获得的价值。

请帮忙。谢谢。

4

1 回答 1

1

当您获得 JSON 数组时,您只需设置 this.people = [来自 JSON 的数据]。如果代码包含在相同的顶级类型 (enyo.sample.RepeaterSample) 中,则此方法有效。

如果 JSON 返回的类型不同,那么您可能需要为 enyo.sample.RepeaterSample 设置一个已发布的属性:

published: {
   people: []
}

然后你可以使用 setPeople(arrayOfPeople)。然后,您还可以在 peopleChanged 方法中自动重新构建转发器:

peopleChanged: function() {
   this.$.repeater.setCount(this.people.length);
}
于 2012-09-06T15:26:27.367 回答