2013-03-12 64 views
1

對不起,如果這是不正確的措辭,這很難解釋。基本上我有一個在他們旁邊有指定日期的項目列表。如果他們超過30分鐘,那麼他們會變紅。完美的作品,但它不是實時的。需要刷新才能看到該項目變爲紅色。Ember.JS綁定時間?

{{#each callback in App.callbackController}} 
    <tr> 
     <td align="center" {{bindAttr style="callback.urgent"}}>{{callback.formatted_time}}</td> 
    </tr> 
{{/each}} 

App.Callback = Ember.Object.extend({ 
    date_time: null, 
    urgent: function() 
    { 
     current_time = new Date().getTime(); 
     callback_time = new Date(this.get('date_time')).getTime(); 

     if(((callback_time - current_time)/1000/60 * -1) > 30) 
     { 
      return 'color: #FFF; font-weight: bold; background: #'+Helpers.colours.red; 
     } else { 
      return 'color: #FFF; font-weight: bold; background: #'+Helpers.colours.green; 
     } 
    }.property('date_time') 
}); 

是否有反正不斷刷新緊急值?

回答

0

您的計算屬性只查看「date_time」屬性,它在您的示例中看起來像是創建項目的時間。

您應該在應用程序某處添加一個「date_time_now」屬性並將其添加到您的計算屬性中。不知道如果燼有一個內置的「現在」屬性,但不應該太難以使用setTimeout()自己設置。

編輯:

重讀我的答案,意識到我沒有解釋它,以及我能有。由於您的計算屬性只會查看date_time(從不更改),因此計算屬性會默認緩存,因此它將被計算一次並緩存。添加date_time_now屬性並更新它將在每次更改date_time_now時強制重新計算。

1

Richard Livsey編寫了a wonderful blog post,它基本上增加了一個應用程序範圍的Clock模型,它通過注入連接到所有控制器。然後,您的計算屬性可以綁定到時鐘屬性,並在適當的時候「喚醒」。

+0

非常酷! – mlienau 2013-03-12 21:54:08