在骨干模型保存都没法'得到' [英] No method 'get' on backbone model save
问题描述
我使用的骨干一个相当复杂的形式。我有一些嵌套模型,并在像这样父模型已经计算其他变量:
// INSIDE父模型computedValue:功能(){
。VAR值= this.get('childModel')得到('childModelProperty');
返回值;
}
这似乎很好地工作保持我的用户界面同步,但只要我称之为
.save()
这是父模型,我得到:
未捕获类型错误:对象#<对象>没有方法'得到'
似乎子模型那种暂时停止响应。
我做得不对本质?
编辑:堆栈跟踪是:
未捕获类型错误:对象#<对象>没有方法'得到'publish.js:90
Backbone.Model.extend.neutralDivisionComputer publish.js:90
Backbone.Model.extend.setNeutralComputed publish.js:39
Backbone.Events.trigger Backbone.js的:163
_.extend.change Backbone.js的:473
_.extend.set Backbone.js的:314
_.extend.save.options.success Backbone.js的:385
f.Callbacks.o jquery.min.js:2
f.Callbacks.p.fireWith jquery.min.js:2
W¯¯jquery.min.js:4
f.support.ajax.f.ajaxTransport.send.d
编辑#2响应如下评论:
有一些基本的我还是没有得到。我换过几个引用this.get('childModel')['childModelProperty']现在我得到的东西像
不能读的未定义的属性childModelProperty。
我还没有从服务器上拉什么,父模型只是创建像
定义(['jQuery的','底线','骨干','模型/ childmodel'],功能($,_,骨干,ChildModel){
VAR ParentModel = Backbone.Model.extend({
默认值:{
childModel:新ChildModel()
}
默认
在创建模型时使用。保存被称为后,它会调用设置
这将覆盖childModel用一个简单的JavaScript对象。在我看来,你有两个选择:
1)使用 Backbone.Relational
2)重写中的每个父模型更新现有的子模型(或创建一个)像下面这样设置
:
儿童:{
childModel:ChildModel
}
设置:功能(键,值,选项){
VAR ATTRS;
如果(_.isObject(密钥)||键== NULL){
ATTRS =键;
选项=价值;
}其他{
的attrs = {};
ATTRS [关键] =价值;
} _.each(this.children,功能(childType,名){
如果(!attrs.hasOwnProperty(名))
返回; //假设孩子只是一个模型 - 不是一个集合
VAR为newValue = ATTRS [名]
删除ATTRS [名]
VAR isModel =本[名]放大器;&安培;这个[名] .SET;
如果(isModel&安培;&安培;为newValue){
这个[名] .SET(为newValue,期权);
}
否则,如果(newValue)以{
这是[NAME] =新childType(为newValue);
}
其他{
删除[名]
} this.trigger('的变化:'+名); }, 这个); 返回Backbone.Model.prototype.set.call(这一点,ATTRS,期权); }
I'm using backbone for a reasonably complicated form. I have a number of nested models, and have been computing other variables in the parent model like so:
// INSIDE PARENT MODEL
computedValue: function () {
var value = this.get('childModel').get('childModelProperty');
return value;
}
This seems to work fine for keeping my UI in sync, but as soon as I call
.save()
on the parent model, I get:
Uncaught TypeError: Object #<Object> has no method 'get'
It seems that the child model kind of temporarily stops responding.
Am I doing something inherently wrong?
EDIT: The stack trace is:
Uncaught TypeError: Object #<Object> has no method 'get' publish.js:90
Backbone.Model.extend.neutralDivisionComputer publish.js:90
Backbone.Model.extend.setNeutralComputed publish.js:39
Backbone.Events.trigger backbone.js:163
_.extend.change backbone.js:473
_.extend.set backbone.js:314
_.extend.save.options.success backbone.js:385
f.Callbacks.o jquery.min.js:2
f.Callbacks.p.fireWith jquery.min.js:2
w jquery.min.js:4
f.support.ajax.f.ajaxTransport.send.d
EDIT #2 in response to comment below:
There's something basic I'm still not getting. I replaced a few references to this.get('childModel')['childModelProperty'] and now I get things like 'cannot read property childModelProperty of undefined.
I'm not yet pulling anything from the server, the parent model is just created like
define(['jquery', 'underscore', 'backbone', 'models/childmodel'], function($, _, Backbone, ChildModel) {
var ParentModel = Backbone.Model.extend({
defaults: {
childModel : new ChildModel()
}
defaults
are only used when you create the model. After save is called, it will call set
which will overwrite childModel with a simple javascript object. As I see it you have a couple options:
1) use Backbone.Relational
2) override set
in each parent model to update the existing child model (or create it) something like the following:
children:{
childModel: ChildModel
}
set: function (key, value, options) {
var attrs;
if (_.isObject(key) || key == null) {
attrs = key;
options = value;
} else {
attrs = {};
attrs[key] = value;
}
_.each(this.children, function (childType, name) {
if (!attrs.hasOwnProperty(name))
return;
//assume the child is just a model--not a collection
var newValue = attrs[name];
delete attrs[name];
var isModel = this[name] && this[name].set;
if (isModel && newValue) {
this[name].set(newValue, options);
}
else if (newValue) {
this[name] = new childType(newValue);
}
else {
delete this[name];
}
this.trigger('change:' + name);
}, this);
return Backbone.Model.prototype.set.call(this, attrs, options);
}
这篇关于在骨干模型保存都没法'得到'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!