骨干模板没有改变

时间:2014-08-21 21:37:39

标签: backbone.js backbone-views backbone-events

我有一个事件的视图。

events: {
    'click button.add-prediction': 'addPrediction'
},
addPrediction: function(event) {
    var currentPredictions = this.model.get('count');
    this.model.set('count', currentPredictions + 1);

    this.$el.find('.add-prediction').html('predicted').addClass('selected');
},

这样做:

this.$el.find('.add-prediction').html('predicted').addClass('selected');

似乎不是正确的方法,但是当我这样做时,

$(event.target).html('predicted').addClass('selected');

DOM没有更新,为什么?我做错了什么?

这是完整视图。

Event.Views.PredictionsView = Backbone.View.extend({
tagName: 'div',
className: 'stats',
events: {
    'click button.add-prediction': 'addPrediction',
    'click button.add-prediction.selected': 'removePrediction'
},
initialize: function() {
    this.model.on('change', this.render, this);
},
removePrediction: function() {
    var currentPredictions = this.model.get('count');
    this.model.set('count', currentPredictions - 2);

    this.$el.find('.add-prediction').html('Predict').removeClass('selected');
    this.$el.removeClass('selected');
    this.$el.parent().find('.add-prediction').removeAttr('disabled');
},
addPrediction: function() {
    var currentPredictions = this.model.get('count');
    this.model.set('count', currentPredictions + 1);

    this.$el.find('.add-prediction').html('Remove prediction').addClass('selected');
    this.$el.addClass('selected');
    this.$el.parent().find('.add-prediction:not(.selected)').attr('disabled', 'true');

},
setTemplate: function() {
    var that = this;
    $.get('templates/singleEvent/team-details.html', function(data) {
        that.template = _.template(data);
        that.render();
    }, 'html');
    return this;
},
render: function() {
    var eventTemplate = this.template(this.model.toJSON());
    $(this.el).html(eventTemplate);
    return this;
}

});

不幸的是我不能制作一个jsFid,因为它过于复杂并且使其工作需要很长时间。 谢谢。

1 个答案:

答案 0 :(得分:1)

event.target不要参考按钮。

event.target [0]指按钮。

尝试

$(event.target[0]).html('predicted').addClass('selected');

event.target.html('predicted').addClass('selected');