在骨干视图中使用Backbone.history.navigate上的变量

时间:2014-04-24 22:13:53

标签: javascript backbone.js underscore.js

如何在Backbone视图中使用变量?

如果我这样工作(如下)......这不起作用。

App.Views.Incentive = Backbone.View.extend({

        el: $('#projet-page'),
        name: "Incentive",
        initialize: function () {
            "use strict";
            $('section[data-projet-page="' + this.name + '"] a.OpenGalerie').on('click', this.enterGalerieClick);
        },
        enterProjectClick: function () {
            Backbone.history.navigate('!/case-projet/' + this.name + '/Galerie'),
            $('section[data-projet-page="' + this.name + '"]').css('display', 'block'),
        },

    });
我有:

域名:9000 /#!/ case-projet // Galerie 而不是域名:9000 /#!/ case-projet / Incentive / Galerie

1 个答案:

答案 0 :(得分:0)

您可以使用下划线的bind方法(请参阅documentation)来解决当前问题,以确保点击处理程序中的this引用Backbone视图,而不是您所使用的元素将处理程序绑定到:

App.Views.Incentive = Backbone.View.extend({

        el: $('#projet-page'),
        name: "Incentive",
        initialize: function () {
            "use strict";
            $('section[data-projet-page="' + this.name + '"] a.OpenGalerie').on('click', _.bind(this.enterGalerieClick, this));
        },
        enterProjectClick: function () {
            Backbone.history.navigate('!/case-projet/' + this.name + '/Galerie'),
            $('section[data-projet-page="' + this.name + '"]').css('display', 'block'),
        },

    });