简体   繁体   English

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

[英]Backbone refresh collection with event handler

I'm using backbone and I'm very new at it, I have a list of products sizes and a list of quantities / prices. 我正在使用骨干网,但我对此很新,我有一个产品尺寸清单和一个数量/价格清单。 When someone selects a different product size, I use backbone to do an ajax call to the server to get me an updated price list. 当有人选择不同的产品尺寸时,我使用中枢对服务器进行ajax调用以获取更新的价格清单。

I'm struggling to get the save function to work so that I can return the updated collection. 我正在努力使save函数起作用,以便我可以返回更新的集合。 I will have to pass back a couple params, but for the time being, I'm just trying to get it to save to the backend. 我将不得不传递一些参数,但是就目前而言,我只是试图将其保存到后端。 I've read save can be used to automatically setup the ajax request. 我已经阅读过save可以用来自动设置ajax请求。

I'd also only like this to load the template when the li element is clicked, not on page load. 我也只希望在单击li元素时加载模板,而不是页面加载。

My code 我的密码

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'
        }  

});

View 视图

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);
              }
          });
    },

});

Page

<script>   
    var prices = new models.PriceList({productId:${productInstance.id}});
    var priceView = new PriceView({collection: prices});
<%--        prices.fetch({reset: true});--%>
</script>

The error I'm getting. 我得到的错误。

TypeError: this.collection.save is not a function TypeError:this.collection.save不是一个函数

this.collection.save({},{ this.collection.save({},{

How do I pass back a couple of params and then refresh the template? 如何传递一些参数,然后刷新模板?

解决方案是使用

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

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

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