gpt4 book ai didi

javascript - 使用 Backbone 调用下划线模板内的函数

转载 作者:可可西里 更新时间:2023-11-01 01:49:24 26 4
gpt4 key购买 nike

只要我尝试做的一件事就能真正简化我现在的生活。

我该怎么做:

这是我在应用文件中的 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() %>

关于javascript - 使用 Backbone 调用下划线模板内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18961438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com