我有一个非常简单的主干js结构。
var Step1View = Backbone.View.extend({
el:'.page',
render:function () {
var template = _.template($('#step1-template').html());
this.$el.html(template);
}
});
var step1View = new Step1View();
var Router = Backbone.Router.extend({
routes:{
"":"home"
}
});
var router = new Router;
router.on('route:home', function () {
step1View.render();
})
Backbone.history.start();
这工作得很好,但是我无法调用这个简单的 jquery 函数。
$(文档).ready(函数() { $('.tip').tooltip(); });
更新
这里是男生错误。 Jquery onload 函数需要放置在路由中。我对 Backbone 很陌生,所以我不确定这是否是最佳实践。但以下有效。
render:function () {
var that = this;
var savings = new Savings();
savings.fetch({
success:function () {
var template = _.template($('#step3-template').html(), {savings:savings.models});
that.$el.html(template);
// put your jquery good ness here
$('.tip').tooltip();
$(".step3-form").validate();
}
})
}
最佳答案
看来您找到了答案!只是想分享一下,您可以通过这样做来缩小 jQuery 的范围。
savings.fetch({
success:function () {
var template = _.template($('#step3-template').html(), {savings:savings.models});
that.$el.html(template);
that.$el.find('.tip').tooltip();
that.$el.find(".step3-form").validate();
}
您的示例中的内容有效,但它每次都会使用 tip
类扫描整个文档以查找 HTML,您可以使用刚刚创建的元素向下扫描,仅查找您刚刚的提示在其内部创建。轻微优化。
希望这对您有帮助!
关于backbone.js - 将 twiiter 工具提示与backbone.js 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13135038/