2016-02-17 42 views
2

嘿所以我嘗試過在旋轉不同的成分數據的重複列表,所以說,我有我的控制器類如何過得慢燼組件數據

a: { 
    name: "a", 
    number: 6 
} 
b: { 
    name: "b", 
    number: 15 
} 

幾個對象,我想它們傳遞到組件模板文件:

{{my-component data=a}} 

但我想通過我的成分在「A」正在顯示4秒的數據的循環中的數據,那麼,「b」是的數據顯示4秒鐘,然後持續重複此操作直到頁面被更改。關於我如何做到這一點,以及我嘗試過但沒有奏效的一些想法。使用Ember.run.later並嘗試調用該函數(我得到'self.function()'不是函數的錯誤(注意這是在控制器的actions部分,它會在您開始旋轉時點擊一個按鈕):

function: function(){ 
    var self = this; 
    return self.get('test'); 
    Ember.run.later((function() { 
    if(self.get('test')) 
     self.set('test', false); 
    else 
     self.set('test', true); 
    self.function(); // <-- Error is here 
    }), 4000); 
} 

,然後使用,如果在模板聲明:

{{#if test}} 
     {{my-component data=a}} 
    {{else}} 
     {{my-component data=b}} 
    {{/if}} 

所以它運行這是第一次,但它從來沒有真正保留通過這兩個數據項旋轉。 非常感謝。

+0

如何直接在組件中傳遞數組,並讓組件在數組中循環。 http://emberjs.jsbin.com/vilode/2/edit?html,js,output – blessenm

回答

2

您可以更改傳遞到組件的數據。

{{my-component data=selectedObject}} 

在你的控制器:

selectedObject: {}, 
count: 0, 
allObjects: [ 
    { 
     name: "a", 
     number: 6 
    }, 
    { 
     name: "b", 
     number: 6 
    } 
], 
rotateObject: function() { 
    this.set('selectedObject', this.get('allObjects')[count]); 
    this.set('count', this.get('count')+1); 
    Ember.run.later(function(){ 
     this.rotateObject(); 
    },4000); 
} 

這樣,每隔4秒selectedObject將發生變化,該組件將用新的數據更新。 我將對象更改爲一個數組。我確信你可以找出一個方法。