我尝试做的一件事实际上会真正简化我的生活。
我怎样才能做到这一点 :
这是我在应用程序文件中的 View
window.ArtView = Backbone.View.extend({
template:_.template($('#art').html()),
render:function (eventName) {
var output="blablbla";
$(this.el).html(this.template({"output":output}));
return this;
}
});
...
// function that I would like to call
function callFunction(){
console.log('it works!');
}
index.html中的模板
<script type="text/tempate" id="art">
<div data-role="header" class="header" data-position="fixed">
<a href="#" data-icon="back" class="back ui-btn-left">Back</a>
</div>
<div data-role="content" class="content">
callFunction();
<% console.log(output) %>
</div>
</script>
如何在模板中调用callFunction()或类似的东西?
任何的想法 ?
谢谢 !
最佳答案
我相信您可以在模板内调用函数,只要模板的对象具有该功能即可。
render:function (eventName) {
var output="blablbla";
var data = _.extend({"output":output}, callFunction);
$(this.el).html(this.template(data));
return this;
}
然后在您的模板中:
<%= callFunction() %>