在每种方法中,我使用this
并希望它指向对象,我是否需要使用_.bindAll()
。如果没有,在什么情况下我需要使用bindAll()
?
var AccountView = Backbone.View.extend({
Name: 'AccountView',
P: {
pictures: $A.Reg.get('path_pictures'),
images: $A.Reg.get('path_images')
},
E: {
hold_name: '#hold_name',
hold_pic_small: '#hold_pic_small',
hold_pic_large: '#hold_pic_large'
},
initialize: function () {
$A.morph(this.E, $A.el);
_.bindAll(this, 'render');
if (_.isObject(this.model)) {
this.obj = this.model;
} else {
this.obj = this.model.toJSON();
}
this.render();
},
render: function () {
var time = new Date().getTime();
this.E.hold_name.innerHTML = this.obj.name;
$A.el('#p' + this.obj.privacy).checked = true;
if (this.obj.picture === 0) {
this.E.hold_pic_small.src = this.P.images + 'generic_small.jpg' + '?_time=' + time;
this.E.hold_pic_large.src = this.P.images + 'generic_large.jpg' + '?_time=' + time;
} else {
this.E.hold_pic_small.src = this.P.pictures + this.obj.h_file + '-1.jpg' + '?_time=' + time;
this.E.hold_pic_large.src = this.P.pictures + this.obj.h_file + '-2.jpg' + '?_time=' + time;
}
}
});
答案 0 :(得分:0)
需要将方法绑定到Backbone类的情况:
您不需要绑定的案例:
例如,在您的情况下,您不需要绑定渲染,因为它被称为this.render,并且您没有任何其他侦听器会导致渲染器丢失范围。