删除Emberjs中的记录 [英] Delete Record in Emberjs

查看:124
本文介绍了删除Emberjs中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力追踪 ember 2.0的文档,用于删除记录,然后重定向到新的URL。当我尝试,我得到以下错误到控制台:

I am struggling to follow ember 2.0's documentation for deleting records and then redirecting to a new url. When I try, I get the following error to the console:

Error while processing route: pencils Attempted to handle event `pushedData` on <name-emberjs@model:pencil::ember523:null> while in state root.deleted.inFlight.  Error: Attempted to handle event `pushedData` on <name-emberjs@model: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;

routes / pencilview.js

routes/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){
          })
      }
  }
});

templates / pencilview.hbs

templates/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}}

controllers / *

controllers/*

all missing except for pencilcreate.js, not applicable here

template / pencils.hbs

template/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>

routes / pencil.js

routes/pencil.js

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


推荐答案

这是另一个答案的版本,只是收紧有一点。

This is a version of another answer, just tightened up a bit.

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

这篇关于删除Emberjs中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆