2015-09-29 59 views
0

我很努力地按照ember 2.0's documentation刪除記錄,然後重定向到一個新的網址。當我嘗試時,我得到以下錯誤到控制檯:在Emberjs刪除記錄

Error while processing route: pencils Attempted to handle event `pushedData` on <[email protected]:pencil::ember523:null> while in state root.deleted.inFlight. Error: Attempted to handle event `pushedData` on <[email protected]:pencil::ember523:null> while in state root.deleted.inFlight. 

我的文件如下。

途徑:

import Ember from 'ember'; 
import config from './config/environment'; 

var Router = Ember.Router.extend({ 
    location: config.locationType 
}); 

Router.map(function() { 
    this.resource('pencilview', { path: '/pencils/:pencil_id' }); 
    this.resource('pencilcreate', { path: '/pencils/new' }); 
    this.resource('pencils'); 
}); 


export default Router; 
import Ember from 'ember'; 
import config from './config/environment'; 

var Router = Ember.Router.extend({ 
    location: config.locationType 
}); 

Router.map(function() { 
    this.resource('pencilview', { path: '/pencils/:pencil_id' }); 
    this.resource('pencilcreate', { path: '/pencils/new' }); 
    this.resource('pencils'); 
}); 


export default Router; 

路由/ pencilview.js

export default Ember.Route.extend({ 
    model: function(params) { 
     return this.store.find('pencil', params.pencil_id); 
    }, 

    actions: { 
     save(pencil){ 
      this.store.find('pencil', pencil.id).then(function(pencil){ 
       pencil.save(); 
      }) 
      this.transitionTo('pencils'); 
     }, 

     remove(id){ 
      this.get('store').find('pencil', id).then(function(pencil2){ 
       pencil2.destroyRecord(); 
      }); 
      this.transitionTo('pencils'); 
     }, 

     cancel(pencil){ 
      this.store.find('pencil'.pencil.id).then(function(pencil){ 
      }) 
     } 
    } 
}); 

模板/ pencilview.hbs

<h2>Single Pencil View</h2> 
<p>Pencil ID: {{model.id}}</p> 

<p> 
<label for='name'>Name</label> 
{{input type="text" id="name" value=model.name}}</p> 

<p> 
<label for='name2'>Name2</label> 
{{input type="text" id="n2" value=model.n2}}</p> 

<p> 
<label for='name3'>Name3</label> 
{{input type="text" id="n3" value=model.n3}}</p> 

<p><button {{action "remove" model.id}}>Delete</button></p> 
<p><button {{action "save" model}}>Save</button></p> 

{{#link-to 'pencils'}}Pencils{{/link-to}} 

控制器/ *

all missing except for pencilcreate.js, not applicable here 

模板/ pencils.hbs

<h2>All Pencils</h2> 
<p>{{#link-to 'pencilcreate' (query-params direction='pencils') class='btn btn-default' }}Create New Pencil{{/link-to}}</p> 

<table id='pencil_allTable' class='display'> 
<thead><tr><td>ID</td><td>Brand</td><</tr></thead> 
<tbody> 
{{#each model as |pencil|}} 
    <tr> 
    <td>{{#link-to "penciliew" pencil class='btn btn-default'}}{{pencil.id}}{{/link-to}}</td> 
    <td>{{pencil.brand}}</td> 
    </tr> 
{{/each}} 
</tbody></table> 

<script> 
$(document).ready(function() { 
    $('#pencil_allTable').DataTable(); 
    }); 
</script> 

路線/ pencil.js

export default Ember.Route.extend({ 
    model() { 
    return this.store.findAll('pencil'); 
    } 
}); 
+0

查看我的回答。 –

+0

亨利和torazaburo的答案似乎並沒有更新我的藥物觀點。也就是說,'delete api'被擊中,但商店沒有更新。當轉換到「鉛筆」時,它會顯示舊數據,除非我刷新頁面。我將獎賞賞金給誰更新他們的答案,並首先解決這個最後的難題。如果一個新的答案跳入下面列出的一個集團,我會檢查它並給亨利獎金。 – mh00h

+0

我對任何「藥物」視圖都沒有看到任何地方。如果你的意思是鉛筆,請顯示路線,控制器和模板。 –

回答

8

這是另一個答案的版本,只是收緊了一下。

remove(id) { 
    this.get('store').find('pencil', id) . 
    then(pencil2 => pencil2.destroyRecord()) 
    .then(() => this.transitionTo('pencils')); 
} 
+0

請參閱我上面的評論以獲得您的答案的進一步完善請求。 – mh00h

3

您已經通過this範圍在被咬傷:

this.transitionTo('pencils').then(function(id){ 
    this.get('store') // <== this === undefined 

此代碼應工作:

remove(id) { 
    this.get('store').find('pencil', id).then(pencil2 => { 
     pencil2.deleteRecord(); 
    }); 
    this.transitionTo('pencils'); 
}, 
+0

這導致GET GET http://0.0.0.0:6543/api/pencils/%3Cname-emberjs%40route% 3Apencils%3A%3Aember590%3E 500(內部服務器錯誤)'。我想知道是否需要在transitionTo之前運行save(),但這會產生一個不同的問題:'處理路由時出錯:pencils嘗試在上處理事件pushData while在狀態root.deleted.inFlight中。錯誤:試圖在上處理事件pushData,同時處於root.deleted.inFlight狀態。 – mh00h

+0

答覆已更新。您無法獲取已刪除的記錄,因此刪除後無法保存。 –

+2

這只是部分作品。調用deleteRecord()不會將'DELETE'請求發送給api;它隻影響當地的商店。將'deleteRecord()'改爲'destroyRecord()'會導致它重新出錯,並記錄'root.deleted.inFlight'錯誤。 – mh00h

3

讓承諾履行本身:

remove(id){ 
     this.get('store').find('pencil', id).then((pencil2) => { 
      pencil2.destroyRecord().then(() => { 
       this.transitionTo('pencils'); 
      }); 
     });   
    }, 

編輯:擺脫const _this的。

+2

好的答案,再加上一個,但是很小的挑剔,因爲我們爲了能夠使用ES6和胖箭頭而付出了所有的麻煩,難道我們不能收緊代碼並同時擺脫'_this'嗎?另外,它可能會稍微更具可讀性而不是嵌套它們。 –

+0

請參閱我上面的評論以獲得您的答案的進一步完善請求。 – mh00h