2012-09-06 82 views
0

我有一個數組。我想在enyo.js中給出數組的值。如何在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獲得。

請幫忙。謝謝。

回答

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); 
} 
+0

如果你把你的代碼在jsfiddle.net(選擇Enyo +庫),那麼我或其他人可以幫助更好一點。 – sugardave