Backbone.js 将模型添加到集合时主干获取和重置模型
我有一个 包含一个模型的Backbone.js 将模型添加到集合时主干获取和重置模型,backbone.js,underscore.js,backbone-model,Backbone.js,Underscore.js,Backbone Model,我有一个 包含一个模型的 Item = Backbone.Model.extend({....}) Apvdtl = Backbone.Model.extend() 还有一套 Apvdtls = Backbone.Collection.extend({ model: Apvdtl }) 并使用Apvdtl模型填充集合 e、 g 并生成了这个视图 但我想做的是,做出这样的观点 通过获取此服务器上ID为的项目 ApvdtlView=Backbone.View.extend({ 标记名
Item = Backbone.Model.extend({....})
Apvdtl = Backbone.Model.extend()
还有一套
Apvdtls = Backbone.Collection.extend({
model: Apvdtl
})
并使用Apvdtl模型填充集合
e、 g
并生成了这个视图
但我想做的是,做出这样的观点
通过获取此服务器上ID为的项目
ApvdtlView=Backbone.View.extend({
标记名:“tr”
初始化:函数(){
this.model.on('change',this.render,this);
this.template=\模板(“”);
},
render:function(){
set({'id':this.model.get('itemid'));
//item={id:'c4676cef679a11e28b7a3085a942bd8e'}
item.fetch();//但这不会获取数据
//这应该包含我获取后的数据
//项目={“id”:“c4676cef679a11e28b7a3085a942bd8e”,
//“代码”:“prp125”,“描述符”:“纸张”,“价格”:“7.00”}
//生成要渲染的新数据
风险值数据={
“itemid”:item.get('descriptor'),
“数量”:此.model.get('数量')
}
this.$el.html(this.template(data));
//this.el.html(this.template(this.model.toJSON());
归还这个;
}
});
首先Ajax是异步的。因此,您永远不知道响应何时返回,因此最好将其附加到事件
ApvdtlView = Backbone.View.extend({
tagName: 'tr',
initialize: function () {
// This listens to the sync and change event and renders the item
this.listenTo(this.model, 'change sync', this.render);
this.template = _.template('<td> <%=itemid%> </td>' +
'<td><%=qty%></td>');
this.model.fetch();
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
因为它是一个不同的域,因为它违反了跨源策略。您需要使用jsonp
来获取此信息
现在您可以看到在“网络”选项卡中获取的数据,但是抛出了一个错误,因为回调必须首先在服务器端处理Ajax是异步的。因此,您永远不知道响应何时返回,因此最好将其附加到事件
ApvdtlView = Backbone.View.extend({
tagName: 'tr',
initialize: function () {
// This listens to the sync and change event and renders the item
this.listenTo(this.model, 'change sync', this.render);
this.template = _.template('<td> <%=itemid%> </td>' +
'<td><%=qty%></td>');
this.model.fetch();
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
因为它是一个不同的域,因为它违反了跨源策略。您需要使用jsonp
来获取此信息
现在,您可以看到在“网络”选项卡中获取的数据,但由于回调必须在服务器端处理,因此会引发错误。谢谢您的回答!=)但是我的视图(ApvdtlView
)不需要url,因为我只是用它来呈现集合中的“数据”(Apvdtls
)。关于urlhttps://raw.github.com/jrsalunga/prism2/master/www/api/item.json
我只是想“把数据放在那里”,不打算保存。视图的数据(ApvdtlView
)来自两个模型:模型(Item
)和此代码apvdtls。添加([{itemid:'c4676cef679a11e28b7a3085a942bd8e',qty:10}])
,所以我只是为我的视图(ApvdtlView
)构建一个新的数据集,而不是我的“ApvdtlView”模型(
Apvdtl谢谢你的回答!)但是我的视图(ApvdtlView
)不需要url,因为我只是用它来呈现集合中的“数据”(Apvdtls
)。关于urlhttps://raw.github.com/jrsalunga/prism2/master/www/api/item.json
我只是想“把数据放在那里”,不打算保存。视图(ApvdtlView
)的数据来自两个模型:模型(Item
)和此代码apvdtls。添加([{itemid:'c4676cef679a11e28b7a3085a942bd8e',qty:10}])
,因此我只是为我的视图(ApvdtlView
)构建一个新的数据集,而不是我的“ApvdtlView”模型(
apvdtll>)
ApvdtlView = Backbone.View.extend({
tagName: 'tr',
initialize: function () {
// This listens to the sync and change event and renders the item
this.listenTo(this.model, 'change sync', this.render);
this.template = _.template('<td> <%=itemid%> </td>' +
'<td><%=qty%></td>');
this.model.fetch();
},
render: function () {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
urlRoot: 'https://raw.github.com/jrsalunga/prism2/master/www/api/item.json'