2017-09-26 74 views
0

我的MEEN堆棧應用程序中有模塊記錄錯誤。我有一個模型,用於從我的express服務器通過貓鼬抓取數據。Ember模型計算屬性不起作用

我想改變我的模型上的日期屬性,以格式化日期的方式與它在mongo數據庫中的存儲方式不同,以便在頁面上保存一些房地產,例如:Tue Sep 19 2017 3:02:10 GMT-0700 (美國東部時間)至2017-09-19。

我跟着模型上的計算機屬性的Ember文檔,但模板不顯示計算值。

型號:

import DS from 'ember-data'; 
import Ember from 'Ember'; 

export default DS.Model.extend({ 
    error: DS.attr('string'), 
    user: DS.attr('string'), 
    module: DS.attr('string'), 
    date: DS.attr('string'), 
    smallDate: Ember.computed('date', function() { 
    var dateObj = new Date(this.get('date')); 
    return `${dateObj.getFullYear()}-${dateObj.getMonth()}-${dateObj.getDate()}`; 
    }) 
}); 

模板:

<div class="row"> 
    <div class="col-md-8 col-md-offset-2 text-center"> 
    <h2 class="toolTitle">Blame Panel</h2> 
    </div> 
</div> 
<div class="col-md-8 col-md-offset-2"> 
    <table class="table"> 
    <thead> 
     <tr> 
     <th>#</th> 
     <th>User</th> 
     <th>Error</th> 
     <th>Component</th> 
     <th>Date</th> 
     </tr> 
    </thead> 
    <tbody> 
     {{#each model as |blame index|}} 
     <tr> 
      <th scope="row">{{index}}</th> 
      <td>{{blame.user}}</td> 
      <td>{{blame.error}}</td> 
      <td>{{blame.module}}</td> 
      <td>{{blame.smallDate}}</td> 
     </tr> 
     {{/each}} 
    </tbody> 
    </table> 
</div> 

如果我只是使用模型的日期屬性會顯示未格式化的日期罰款。

回答

1

看起來它應該工作。你看到了什麼行爲?

可能更適合您的設置的另一個選項是將該格式化問題移到幫助程序中。如果你這樣做ember g helper format-date,那麼你可以使用同樣的方法來格式化你的日期在這樣一個不同的地方:

{{format-date blame.date}} 
+0

我只是在模板中越來越沒有返回值的。我檢查了餘燼檢查員,並且該財產沒有出現在模型中。 –

+0

我會建議在單元測試中測試東西,然後(助手方法或你的模型)。我發現這對追蹤這些錯誤非常有幫助,因爲您可以添加一個'debugger'語句來查看發生了什麼 – acorncom

+0

感謝您的建議生病嘗試單元測試它,這會讓我感到無法結束,如果我不現在搞清楚了 –