ember.js - emberjs new-router-V3/controller 无法使用 ember-data 创建或编辑记录

标签 ember.js ember-data ember-router

在此jsfiddle ,我有 EmBlog.PostsNewRouteEmBlog.PostsEditRoute。这些路线包含“保存、取消和销毁”事件。

当我创建一条新记录时,它只在内存中创建它,而不会调用 store.commit() ,并且在控制台中,它会抛出错误:

未捕获类型错误:无法调用未定义的“commit”方法

当我尝试编辑时,它会抛出相同的错误,但编辑仍然只发生在内存中。

销毁操作也失败。

当我调用取消时,我得到:

无法读取未定义的属性“defaultTransaction”

大部分代码都在jsfiddle。保存和取消事件遵循 Yehuda here 描述的模式。 :

    App.NewUserRoute = Ember.Route.extend({
      model: function() {
         return App.User.createRecord();
      },

     events: {
         save: function(user) {
          this.get('store').commit();
         }
     }
   });

谢谢

最佳答案

已更新fiddle !它现在用于创建、编辑和销毁用例。有关我更改的详细信息,请参阅下文...

When I create a new record, it only creates it in memory and never calls the store.commit() and in the console, it throws the error: Uncaught TypeError: Cannot call method 'commit' of undefined

原来的PostNewRoute失败,因为this.store未定义。而且 this.content 也将是未定义的。

save: function(post) {
  this.store.commit();
  this.content.addObserver('id', this, 'afterSave');
 },

更新后的版本调用帖子事务的提交。还使用 post.one 回调在创建记录后进行转换。

 save: function(post) {
   post.one('didCreate', this, function(){
     this.transitionTo('posts.show', post);
   });
   post.get('transaction').commit();
 },

稍后将更新其他更新的详细信息...

When I try to edit, it throws thesame error but then the edit still happens only in memory.

The destroy action also fails.

When I call cancel, I get: Cannot read property 'defaultTransaction' of undefined

关于ember.js - emberjs new-router-V3/controller 无法使用 ember-data 创建或编辑记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14431010/

相关文章:

ember.js - 从验收测试中触发输入的操作?

ember.js - Ember cli 适配器为类型设置自定义路径

javascript - Ember 路由模型阻止 javascript

javascript - Ember.js:如何在 Javascript 代码中访问模型的属性

javascript - EmberJS 在不同上下文中使用组件

ember.js - 使用 ember-cli 阻止助手

ember.js - 在 Ember-CLI 中创建一个将迭代一定次数的 for 循环

ember.js - "Error while processing route"原因不明

javascript - ember linkTo 在路由的模型 Hook 中发送空参数

javascript - Ember : How do I filter users by any property using routes