I'm very new to backbone but I manage to get it working from tutorial. But when I want to port those application to the backbone boilerplate I find myself stuck at the template. How do I access my model from my template? or even from the js file itself? I find myself stuck here for awhile now.
Backbone Boilerplate refers to https://github.com/tbranyen/backbone-boilerplate
For most javascript templating libraries, templating occurs in two phases.
.
//Compile your template string into a function
//Happens 1 time only then you can cache the function
var templateFunction = _.template("<p>Your <%- part %> is so <%- description %></p>");
//Generate your output HTML with varying sets of data.
var html1 = templateFunction({part: "nose", description: "big"});
//html1 has "<p>Your nose is so big</p>";
var html2 = templateFunction({part: "cat", description: "fat"});
//html2 has "<p>Your cat is so fat</p>";
This is the same basic idea for underscore templates, JST, jade, and most other templating engines. The "context data" is how your template gets access to your model. If you want, you can give it direct access to the underlying model by providing a context like: {model: myModel};
. Then in your template you could do something like <%= model.get("displayName") %>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With