【发布时间】:2015-06-22 20:38:23
【问题描述】:
我正在使用骨干网,而且我很新,我有一份产品尺寸清单和一份数量/价格清单。当有人选择不同的产品尺寸时,我使用主干对服务器进行 ajax 调用以获取更新的价格表。
我正在努力让保存功能正常工作,以便我可以返回更新后的集合。我将不得不传回几个参数,但目前,我只是想让它保存到后端。我读过 save 可用于自动设置 ajax 请求。
我也只希望在单击 li 元素时加载模板,而不是在页面加载时加载。
我的代码
var models = {};
models.PriceModel = Backbone.Model.extend({
})
models.PriceList = Backbone.Collection.extend({
initialize: function(options) {
this.productId = options.productId;
},
model: models.PriceModel,
url: function() {
return '../product/pricing/' + this.productId + '.json'
}
});
查看
var PriceView = Backbone.View.extend({
el: '#product-module',
template: Handlebars.compile($("#priceTemplate").html()),
events: {
"click #product-dimensions li": "dimensionClicked",
},
initialize: function(){
this.listenTo(this.collection, 'add', this.render);
this.listenTo(this.collection, 'reset', this.render);
},
render: function() {
this.$el.find('#product-quantities').html( this.template(this.collection.toJSON()));
},
dimensionClicked: function(event, callback){
this.collection.save({},{
success: function(model, data){
console.log('success')
this.collection.fetch();
},
error: function(model, response) {
console.log('error! ' + response);
}
});
},
});
页面
<script>
var prices = new models.PriceList({productId:${productInstance.id}});
var priceView = new PriceView({collection: prices});
<%-- prices.fetch({reset: true});--%>
</script>
我得到的错误。
TypeError: this.collection.save 不是函数
this.collection.save({},{
如何传回几个参数然后刷新模板?
【问题讨论】:
-
您的意思是使用 collection.sync 吗?
-
罗曼,你能解释一下吗?我不完全确定我应该使用什么,这是我第一次使用主干。
-
我不确定您要在这里完成什么。维度点击有什么作用?我相信您必须获取使用 $(event.currentTarget) 单击的维度,然后对其进行处理。您是否应该根据它更改网址?然后你应该分配collection.url并调用collection.fetch()。
-
另外,我不需要将产品集合传回服务器,我只需将产品id和尺寸id传给服务器,然后我将执行一些价格计算并返回我的数量集合..
-
两个单选组,一个有产品尺寸,另一个有产品数量,当您点击不同的产品尺寸时,您将触发方法dimensionclicked,然后将产品ID和尺寸值发送到将返回更新的数量/价格集合的后端。