2013-05-06 18 views
4

如何推遲呈現,直到在Marionette中獲取模型?我可以在模型上聽取更改事件,但它會呈現兩次。有沒有優雅的解決方案?MarionetteJS:僅在獲取模型成功後呈現

Manager.module 'Views', (Views, App, Backbone, Marionette, $) -> 
    class UserDetail extends Marionette.ItemView 
     template: 'manager.users.detail' 


     initialize: => 
      @model = new App.Models.ManagerUser() 
      return 

     onBeforeRender: => 
      @username = 'test' # Comes from URL 
      @model.fetch() 
      return 

     serializeData: -> 
      data = @model.toJSON() 
      return data 

回答

4

一個常見的方法是從您的控制器而不是視圖中調用fetch。這樣可以避免在視圖中處理路由事件。

這是演示該技術的小提琴。

http://jsfiddle.net/puleos/PHpCz/

編輯:(小提琴更新)

Mod.metaModel = new metaModel(); 
Mod.tagsCollection = new tagsCollection(); 

Mod.compositeView = new CompositeView({ 
    model: Mod.metaModel, 
    collection: Mod.tagsCollection 
});  

var metaPromise = Mod.metaModel.fetch({dataType: "jsonp"}); 
var tagsPromise = Mod.tagsCollection.fetch({dataType: "jsonp"}); 

metaPromise.done(function(data) { 
    App.region.show(Mod.compositeView); 
}); 
+0

一個非常好的方式與不連續取或競爭條件的單一成功實現並行抓取是在提到使用$。當這個答案類似的問題:http://stackoverflow.com/a/12168628/271868 – Emeka 2016-01-27 22:37:33