使用主干调用下划线模板内的函数 [英] Call a function inside an underscore template using backbone
本文介绍了使用主干调用下划线模板内的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试做的一件事会真正简化我现在的生活.
我该怎么做:
这是我在应用文件中的视图
window.ArtView = Backbone.View.extend({模板:_.template($('#art').html()),渲染:函数(事件名称){无功输出=blablbla";$(this.el).html(this.template({"output":output}));返回这个;}});...//我想调用的函数函数调用函数(){console.log('它有效!');}
index.html 中的模板
如何在我的模板或类似的东西中调用 callFunction() ?
有什么想法吗?
谢谢!
解决方案
我相信只要模板的对象有函数,你就可以在模板内调用函数.
render:function (eventName) {无功输出=blablbla";var data = _.extend({"output":output}, callFunction);$(this.el).html(this.template(data));返回这个;}
然后在您的模板中:
<%= callFunction() %>
Just a thing I try to do that would really simplify my life right now.
How can I do that :
This is my view in app file
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!');
}
Template in 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>
How can I call callFunction() inside my template or something alike ?
Any idea ?
Thanks !
解决方案
I believe you can call functions within the template as long as the object for the template has the function.
render:function (eventName) {
var output="blablbla";
var data = _.extend({"output":output}, callFunction);
$(this.el).html(this.template(data));
return this;
}
then in your template:
<%= callFunction() %>
这篇关于使用主干调用下划线模板内的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文