繁体   English   中英

具有事件处理程序的主干刷新集合

[英]Backbone refresh collection with event handler

我正在使用骨干网,但我对此很新,我有一个产品尺寸清单和一个数量/价格清单。 当有人选择不同的产品尺寸时,我使用中枢对服务器进行ajax调用以获取更新的价格清单。

我正在努力使save函数起作用,以便我可以返回更新的集合。 我将不得不传递一些参数,但是就目前而言,我只是试图将其保存到后端。 我已经阅读过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({},{

如何传递一些参数,然后刷新模板?

解决方案是使用

this.collection.fetch({data: {customParm : searchData}, reset: true});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM