2013-04-28 56 views
0

我有一個從Ember.JS模型中刪除記錄的問題。EmberJS CRUD:deletedRecord不斷重新顯示後linkTo

我有一個我的把手模板中的記錄概述,每行有一個刪除按鈕。

單擊按鈕時,我想從表中刪除該行(並在我的REST API上執行DELETE)。

我的句柄模板包含下表+每個記錄的刪除按鈕。

<table class="table table-hover"> 
    <tr> 
    <th>Latitude</th> 
    <th>Longitude</th> 
    <th>Accuracy</th> 
    <th></th> 
    </tr> 
    {{#each model}} 
    <tr> 
    <td>{{latitude}}</td> 
    <td>{{longitude}}</td> 
    <td>{{accuracy}}</td> 
    <td><button {{action removeItem this target="view"}}>Delete</button></td> 
    </tr> 
    {{/each}} 
</table> 

我的看法是這樣的

App.LocationsIndexView = Ember.View.extend({ 
    removeItem: function(location) { 
     this.get('controller').send('removeItem', location); 
    } 
}); 

我的控制器看起來是這樣的:

App.LocationsIndexController = Ember.ArrayController.extend({ 
    removeItem: function(location) { 
    console.log("Removing location " + location); 

    location.one("didDelete", this, function() { 
     console.log("record deleted"); 
     this.get("target").transitionTo("locations"); 
    }); 

    location.deleteRecord(); 
    this.get("store").commit(); 
    } 
}); 

的的removeItem方法被調用時,該項目從表中刪除和DELETE被稱爲在REST API上。太好了!

但是......

如果我切換到另一個模板(使用linkTo),並返回到概覽表(再次使用linkTo),已刪除的記錄重新出現在表格中。 它不存在於後臺了,所以不能再刪除:

Uncaught Error: Attempted to handle event `deleteRecord` on <App.Location:ember356:517d96d7658c51de1c000027> while in state rootState.deleted.saved. Called with undefined 

我已經通過了導遊,但他們強迫你所有的地方往下跳。 http://emberjs.com/guides/views/看起來很有前途(實際上顯示了我想要實現的刪除按鈕),只能繼續完全不同的事情。

我想了解爲什麼刪除的項目會重新出現,以及如何避免這種情況。我已經嘗試在ArrayController中調用this.removeObject(location),但刪除的記錄不斷重新顯示。

我可以使用瀏覽器控制檯

  • LOCS = App.Location.find重現問題();
  • locs.content.length //返回5
  • newLoc = App.Location.createRecord({緯度:120,經度:120})
  • LOCS = App.Location.find();
  • locs.content.length //返回6
  • newLoc.deleteRecord()
  • locs.content.length //返回5個
  • LOCS = App.Location.find();
  • locs.content.length //返回6(爲什麼它返回6在這裏?

回答

1

這個問題是由於ember.js /燼-data.js或兼容的錯誤引起的之間的問題2.

當我從builds.emberjs.com網站獲取最新的一對ember和ember-data時,問題得到解決。

採用這種組合,刪除工作得很好並

  • LOCS = App.Location.find();
  • locs.content.length //返回5
  • newLoc = App.Location.createRecord({緯度:120,經度:120})
  • LOCS = App.Location.find();
  • locs.content.length //返回6
  • newLoc.deleteRecord()
  • locs.content.length //返回5個
  • LOCS = App.Location.find();
  • locs.content.length // 回報5,現在,它應該在哪裏這個returnd 6

因此,使用燼數據時,始終確保它與您正在使用的ember.js兼容之前。 EmberJS持續集成在http://builds.emberjs.com上發佈最新版本的ember和ember-data應該通過他們的測試套件,並且應該被認爲是兼容的。