backbone.js - Can I pass a global var to an underscore view? -
backbone.js - Can I pass a global var to an underscore view? -
i wonder if possible pass global variable when rendering template. variable each time phone call controller, looks this:
window.myvar = 0; //this var alter in given moment when create request server.. so: //into render method of view, have this: var template = _.template($("#mytemplate").html(), { variwanttopass : myvar } ); this.$el.html(template);
this way can access template this:
<%= variwanttopass.get('myvar') %>
if possible?; , also, each time render view, code excute 1 time again , update value?
yes, except template has be
<%= variwanttopass %>
and pass variable window.myvar not accidentally replace it
and yes update after each render if pass variable each time
working example:
html:
<script id="mytemplate" type="text/html"> <%= variwanttopass%> </script> <div></div>
js:
window.myvar = 'a'; var templatehtml = $("#mytemplate").html() var render = function () { var template = _.template(templatehtml, { variwanttopass : window.myvar } ); homecoming template; } $('div').html(render()); window.myvar = 'b'; //change variable settimeout(function() { $('div').html(render()); }, 1000)
http://jsfiddle.net/omynhl1d/
however advocate on not using global variable , instead saving somewhere in backbone view or improve model , render listening on model alter event
backbone.js
Comments
Post a Comment