我是Backbone.js的新手,我正在尝试触发视图更改事件toggleShape
,但它没有触发。
Shapes = Backbone.View.extend({
el: '#shape',
initialize: function () {
_.bindAll(this, 'afterRender');
var $el = this.$el;
$.each(App.enums.itemEnums.measurement.shape, function (value, label) {
$el.append(getRadioButton(value, 'measurement', label));
});
this.render();
this.afterRender();
},
events: {
'click input': 'toggleShape'
},
toggleShape: function (e) {
console.log('a');
},
afterRender: function () {
var value = '5';
//$('input[value="' + value + '"]', this.$el).prop('checked', true); - DOES NOT WORK
//this.trigger('change'); - DOES NOT WORK
$('#shape input').eq(0).trigger('click'); // - DOES NOT WORK TOO!
}
});
$(document).on('click', '#shape input', function () {
console.log('b');
});
之后在控制台中,我只能看到b
,而不是ab
当我点击输入时,我会看到一切正常,但如何从视图函数toggleShape
中触发afterRender
?
控制台没有错误。
答案 0 :(得分:1)
您可以在构建视图期间绑定事件。使用UI对象,然后您应该能够使用视图的其他功能调用它。
Shapes = Backbone.View.extend({
el: '#shape',
initialize: function () {
_.bindAll(this, 'afterRender');
var $el = this.$el;
$.each(App.enums.itemEnums.measurement.shape, function (value, label) {
$el.append(getRadioButton(value, 'measurement', label));
});
this.render();
this.afterRender();
},
ui : {
theInputs : '#shape input'
},
events: {
'click @ui.theInputs': 'toggleShape'
},
toggleShape: function (e) {
console.log('a');
},
afterRender: function () {
var value = '5';
this.ui.theInputs.trigger('click'); // - Should Work :)
}
});