2016-04-26 46 views
0

我知道它已被廢棄,但仍無法找到解決方案。如何更新採集數據並使用Backbone Marionette渲染

我的看法工作正常,但事件(updateClicked)我想調用服務器來更新。服務器將返回完整列表。 更新集合並重新呈現視圖的最佳做法是什麼?

感謝

FYI我用requirejs

型號

CommonEntities.Item = Backbone.Model.extend({ 
}); 
CommonEntities.Items = Backbone.Collection.extend({ 
     model: CommonEntities.Item 
}); 

getCommonDataModalAll: function(url) { 
       var defer = $.Deferred(); 
       var ajax = $.ajax({ 
        url: url, 
        type: 'GET' 
       }); 
       ajax.done(function (Data) { 
        var source= new CommonEntities.Item(); 
        source.set(Data); 
        defer.resolve(source); 
       }); 
       ajax.fail(function(jqXHR, textStatus, errorThrown) { 
        defer.reject(errorThrown); 
       }); 
       return defer.promise(); 
      } 

Main.reqres.setHandler("common:getCollection", function(url) { 
     return API.getCommonDataCollection(url); 
    }); 

控制器

var fetchingData = Main.request("common:getCollection",'my_url'); 
       fetchingData.done(function (Collection) { 
        var table = new ListPage.Notes({ 
         collection: Collection 
        }); 
        Main.MainRegion.show(table); 
       }); 

六EW

ListPage.Item = Marionette.ItemView.extend({ 
       initialize:function() { 
       }, 
       className: "note", 
       template: BodyTpl, 
       tagName: "tr" 
      }); 
      ListPage.Notes = Marionette.CompositeView.extend({ 
       template: HeadTpl, 
       tagName: "table", 
       className: "row table table-hover", 
       childViewContainer: "tbody", 
       childView: ListPage.Item, 
       events: { 
        "click .s-update": "updateClicked", 
       }, 
       updateClicked: function (e) { 
        e.stopPropagation(); 
        //some jquery logic 
        //update the server 
        //how to call the server? 
       }, 
      }); 

回答

0

我會做這樣的:

ListPage.Notes = Marionette.CompositeView.extend({ 
    ... 
    initialize: function() { 
     this.listenTo(this.collection, 'sync', this.render); 
    }, 
    ... 
});