0

I'm new to Backbone.js, and trying to wrap my head around the whole thing. We already have a custom template system which we use internally. My issue is that our system requires an async callback to function correctly.

I have read at some other places that Backbone.View will take a jQ defer (although I'm not sure exactly where it consumes it), however I want to know if it is possible otherwise.

Is there a way to populate a Backbone.View with template content loaded via a callback?

1 Answer 1

1

Spot, the logic for Backbone.View's rendering is fully under your control.

What backbone view will do by default when you new it is to create an empty detached DOM element (with the tagName specified in your view, 'div' by default, with the className specified in your view, '' by default). Then, whenever you feel like it you can fill this detached DOM element with HTML Content (or other detached nodes from subviews), and/or attach it to the DOM to get it to display.

What that means for you is that the template key in your View is just a convenient placeholder to put the HTML you want to render, but you don't have to use it, and if you put a value in it, it won't do anything until you use it to update the HTML of your View's el (the DOM element created by your view). So if this key contains a jQ defer, it will work, in the sense that the key will contain the template when it's ready... but it will not magically render anything.

One way (out of many) is to bind the defer's resolve callback (done) to a function that calls the view's render (that's usually how the function that update the DOM is called, but it's just a convention, you can call it _renderASmallPart the template is just a partial) and probably use the arguments to update the view's template key. If you have trouble with having the right context for the callbacks, don't forget the ever helpful _.bindAll!

1
  • Ah, thank you very much. That cleared up a few other small discrepancies in my understanding as well.
    – Spot
    Commented Dec 4, 2013 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.