在backbone.js中使用twiiter工具提示。

问题描述 投票:2回答:2

完整的例子在这里

我有一个非常简单的backbone 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函数。

$(document).ready(function() { $('.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();
                }
            })

        }
backbone.js twitter-bootstrap
2个回答
2
投票

看来你找到了答案! 只是想分享一下,你可以通过这样做来缩小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();
            }

在你的例子中,你有什么工作,但它也是每次扫描整个文档的HTML与类的 tip 其中你可以使用你刚刚创建的元素,只向下扫描你刚刚创建的元素里面的提示。 稍微优化一下。

希望对你有所帮助!


0
投票

看来你找到了答案 只是想分享一下,你可以通过这样做来缩小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();
        }

你在例子中的做法是可行的,但也是每次扫描整个文档中的HTML与类tip,而你可以用你刚创建的元素向下扫描,只扫描你刚在里面创建的tip。稍微优化一下。

希望对你有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.