14 javascript backbone.js underscore.js
我试图这样做的事情真的会简化我现在的生活.
我怎样才能做到这一点 :
这是我在app文件中的视图
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!');
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
如何在模板中调用callFunction()或类似的东西?
任何的想法 ?
谢谢 !
Vic*_*Vic 16
我相信只要模板的对象具有该功能,您就可以调用模板中的函数.
render:function (eventName) {
var output="blablbla";
var data = _.extend({"output":output}, callFunction);
$(this.el).html(this.template(data));
return this;
}
Run Code Online (Sandbox Code Playgroud)
然后在你的模板中:
<%= callFunction() %>
Run Code Online (Sandbox Code Playgroud)
这就是我做的,它运作正常.
template: _.template(templateText , {
imports : {
check :function (val){
// blah blah
}
}
}
}),
Run Code Online (Sandbox Code Playgroud)
然后在你的HTML模板中
<%= check('value') %>
Run Code Online (Sandbox Code Playgroud)