如何在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
答案 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'),
},
});