4

How can I postpone rendering until a model is fetched in Marionette? I can listen to change event on the model, but then it will be rendered twice. Is there an elegant solution?

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

1 回答 1

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);
});
于 2013-05-06T15:22:17.387 回答