You will need to modify the LayoutManger to handle the asynchronous display of templates.
One way to do this is to use the jQuery Deferred object, in which the call to render returns an instance of the Deferred object, which is then resolved when the Dust library is called.
Then modify Backbone.LayoutManager to NOT continue to do what it wants with the templates until this Pending object is resolved.
in render :
render: (template, context) -> dfd = $.Deferred() dust.render @template, context, (err, out) -> throw err if err done(out) dfd.resolve() return dfd;
Then you take the method in Backbone.LayoutManager that calls the rendering and instead of doing something with the return from the rendering, you attach this βsomethingβ to the done handler for the deferred that you passed back.
dfd = render(template, context) dfd.done(do_something_with_this_template);
I use Marionette.js (along with the asynchronous option) and dust, this is what my main render method looks like:
render: function(){ var dfd = $.Deferred(); var template_context = this.model; if(_.isUndefined(template_context) || _.isFunction(template_context)){ template_context = new (Backbone.Model.extend({})); } if(this.id) this.$el.attr('id', this.id); var that = this; dust.stream(this.tpl_name, template_context.toJSON()) .on('data', function(data){ that.$el.html(data); }) .on('end', function(){ dfd.resolve(); that.enable_menus(); that.trigger('template:rendered'); }) .on('error', function(err){ dfd.reject(); window.le(err.message, err); that.trigger('template:error'); }); return dfd; }
(Obviously not using coffeescript either ...)
tkone source share