[英]can't save/update record using ember-data
My Router defines the following: 我的路由器定义以下内容:
this.resource('uoms', { path: '/uoms' }, function() {
this.route('new');
});
And the uoms
route is defined as: uoms
路由定义为:
App.UomsRoute = Ember.Route.extend({
model: function() {
return this.store.find('uom');
},
actions: {
save: function() {
this.modelFor('uoms').save().then(function() {
console.log("saved UOMs");
});
}
}
});
But for some reason when I try and save it I am getting: 但是由于某种原因,当我尝试保存它时,我得到了:
Uncaught TypeError: Object [object Object] has no method 'save'
未捕获的TypeError:对象[object Object]没有方法'save'
Can someone help me identify what I'm doing wrong? 有人可以帮助我确定我在做什么错吗?
---- UPDATE ---- ----更新----
I also tried the following from the controller but with the same results: 我还从控制器尝试了以下操作,但结果相同:
App.UomsController = Ember.ArrayController.extend({
actions: {
save: function() {
this.get('model').save().then(function() {
console.log("saved UOMs");
});
}
}
});
---- UPDATE 2 ---- ----更新2 ----
Looking at the object returned by the this.get('model')
call we get the following: 查看
this.get('model')
调用返回的对象,我们得到以下信息:
This is what I would do: 这就是我要做的:
this.get('model')
the Chrome Developer Tools Console. this.get('model')
。 Check if the console output is really what you expect. 检查控制台输出是否确实是您期望的。 Most probably it is not the model instance you want to save.
很有可能不是您要保存的模型实例。 You will see that you will get the same error when you execute
this.get('model').save()
in the console. 您将在控制台中执行
this.get('model').save()
时看到相同的错误。
BTW: Why are you using an ArrayController
and not an ObjectController
. 顺便说一句:为什么要使用
ArrayController
而不是ObjectController
。 It looks like you use it for a single model. 看起来您将其用于单个模型。 See the docs for more details on that.
有关更多详细信息,请参阅文档 。
Edit: 编辑:
Try this.get('model').get('transaction').commit()
试试
this.get('model').get('transaction').commit()
The problem I was running into was that I was trying to call save()
on an array of records rather than a singular record. 我遇到的问题是我试图在记录数组而不是单个记录上调用
save()
。 This problem was created because I was operating on a singular record but doing this within an ArrayController
. 出现此问题的原因是,我正在对单个记录进行操作,但在
ArrayController
执行了此操作。 I don't think there's anything wrong with this although it could be argued that I should have created a sub-route called "edit" and then presumably my code logic would have worked. 我认为这没有什么错,尽管可以说我应该创建一个名为“ edit”的子路由,然后假定我的代码逻辑会起作用。
That said, if you want to do as I did (aka, save a singular record within an ArrayController) then this code will work: 就是说,如果您想像我一样做(也就是在ArrayController中保存单个记录),那么此代码将起作用:
save: function(id) {
var promise = this.store.find('uom',id).then(function(uom){
console.log(uom);
uom.save();
});
}
And then in the template put something like this: 然后在模板中输入如下内容:
<span class="glyphicon glyphicon-floppy-disk" {{action "save" id}}></span>
This works but is not the best answer. 这有效,但不是最佳答案。 Instead you can specify in the
ArrayController
an ObjectController
with the itemController
property. 相反,你可以在指定
ArrayController
的ObjectController
与itemController
财产。 Below is an example of both a save()
and deleteRecord()
handler using this strategy: 下面是使用此策略的
save()
和deleteRecord()
处理程序的示例:
App.PluralController = Ember.ArrayController.extend({
itemController: 'singular'
});
App.SingularController = Ember.ObjectController.extend({
actions: {
save: function() {
this.get('model').save();
},
deleteRecord: function() {
this.get('model').deleteRecord();
this.get('model').save();
}
}
});
Then you'll want to do something like the following for your handlebars template: 然后,您需要对车把模板执行以下操作:
<span class="glyphicon glyphicon-floppy-disk" {{action "save" this}}></span>
<span class="glyphicon glyphicon-remove-circle" {{action "deleteRecord" this}}></span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.