簡體   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