2013-10-21 26 views
0

糾正我,如果我錯了,但我認爲Ember應該大部分模型 - 視圖綁定爲您? 當您必須手動跟蹤模型更改並相應地更新/刷新視圖時,會出現什麼情況?Ember.js不會自動綁定模型RESTful更改

我正在工作的應用程序嵌套了與它們相關的路線和模型。

App.Router.map(function() { 
    this.resource('exams', {path: "/exams"}, function() { 
     this.resource('exam', {path: ":exam_id"}, function(){ 
      this.resource('questions', {path: "/questions"}, function() { 
       this.route("question", {path: ":question_id" }); 
       this.route("new"); 
      }) 
     }) 
    }); 
}); 

一切工作正常,我能夠從其他服務器分別得到考試和問題。

對於每個模型,我都有適當的Ember.ArrayControllerEmber.ObjectController來處理視圖中的列表和單個模型項目。基本上對於這兩種模型,我處理事物的方式都是IDENTICAL,除了一個嵌套在另一箇中。 另外一個區別是,要顯示嵌套的路線數據,我正在使用另一個{{outlet}} - 第一個模板中的一個。

現在的問題是,綁定到視圖的頂層模型是由Ember自動處理的,沒有任何特殊的觀察者,綁定等。當我添加新項目時,它會被保存,並且刷新列表視圖以反映更改,或者當項目被刪除時,它會自動從視圖中刪除。 「它只適用於(c)」

對於第二個模型(問題),另一方面,我能夠重現所有粗俗行爲,並且它工作正常,但UI不會自動更新以反映更改。

舉例來說,我不得不這樣的事情添加新條目時(這行有一個評論):

App.QuestionsController = Ember.ArrayController.extend({ 
    needs: ['exam'], 
    actions: { 
     create: function() { 
      var exam_id = this.get('controllers.exam.id') 
      var title = this.get('newQuestion'); 
      if (!title.trim()) { return; } 
      var item = this.store.createRecord('question', { 
       title: title, 
       exam_id: exam_id 
      }); 
      item.save(); 
      this.set('newQuestion', ''); 
      this.get('content').pushObject(item); // <-- this somehow important to update the UI 
     } 
    } 
}); 

而這是我處理了(考試模式)

什麼時我做錯了?我如何獲得Ember.js跟蹤和綁定模型併爲我更改UI?

回答

0

隨着

this.get('content').pushObject(item); 

你推到問題控制器陣列的新問題。我認爲如果你直接向考試has_many關係提出新的問題會更好。

exam = this.modelFor('exam'); 
exam.get('questions').pushObject(item);