Backbone - 如何重构此代码以防止ghost视图?

时间:2012-03-26 02:54:30

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

我有一系列与Backbone系列相关的闪存卡。收到集合后,我创建了一个播放器模型的实例。

然后,用户可以使用“下一个”和“上一个”按钮浏览其余的闪存卡。我第一次尝试这样做,我认为很简单就是将flashCards传递给这样的玩家。

不幸的是,这种设计导致每次点击时都会绑定下一个和上一个按钮事件。因此,在第一次单击下一个按钮之后,例如,事件开始多次触发。我在某处读到有关鬼视图的内容,但无法弄清楚如何将下面的代码分解成一个可以帮助我防止ghost视图问题的块。

var flashCards = new Quiz.Collections.FlashCards({
        id: this.model.get('card_set_id')
});

Quiz.player = new Quiz.Models.FlashCardPlayer({
        collection: flashCards
})

Quiz.Models.FlashCardPlayer = Backbone.Model.extend({
defaults: {
    'currentCardIndex': 0
},

initialize: function(){
    this.collection = this.get('collection');
    this.showCard();

},

showCard: function(){
    var flashCard = this.collection.at(this.get('currentCardIndex'));
    var cardView = new Quiz.Views.FlashCardPlayer({
        model: flashCard
    });
},

currentFlashCard: function(){
    return this.get('currentCardIndex');
},

previousFlashCard: function(){
    var currentFlashCardIndex = parseInt(this.get('currentCardIndex'), 10);
    if(currentFlashCardIndex <= 0){
        console.log("no less");
    }
    this.set({
        'currentCardIndex': currentFlashCardIndex--
    });
    this.showCard();
},

nextFlashCard: function(){
    var currentFlashCardIndex = parseInt(this.get('currentCardIndex'), 10);
    if(currentFlashCardIndex >= this.collection.length){
        console.log("no more");
    }
    currentFlashCardIndex = currentFlashCardIndex + 1;
    this.set({
        'currentCardIndex': currentFlashCardIndex
    });
    console.log(this.get('currentCardIndex'));
    this.showCard();
 }

});

Quiz.Views.FlashCardPlayer = Backbone.View.extend({
   el: $('#cardSet'),
   tagName: 'div',
   _template: _.template($('#playerTemplate').html()),

initialize: function(){
    console.log("in view flashcardplayer", this);
    this.render();
},

events: {
    'click #previous': 'getPreviousCard',
    'click #next': 'getNextCard'
},

render: function(){
    $(this.el).html(this._template(this.model.toJSON()));
    return this;
},

getPreviousCard: function(){
    this.close();
    Quiz.player.previousFlashCard();
},

getNextCard: function(){
    this.close();
    Quiz.player.nextFlashCard();
}

});

script#playerTemplate(type="text/template")
<div id="state"></div>
<div id="previous">Previous</div>
<div id="card">
   <h2><%= question %></h2>
    <h3><%= answer %></h3> 
</div>
<div id="next">Next</div>

1 个答案:

答案 0 :(得分:2)

每次展示新卡时,您都会创建Quiz.Views.FlashCardPlayer的新实例。这些实例中的每一个都执行自己的事件处理,因此每个实例都绑定到相同的#next#previous元素。

我认为这里有几个概念问题:

  • 您只需要一个FlashCardPlayer视图,该视图应绑定下一个/上一个元素的事件。你可能应该有一个单独的FlashCard视图,它显示一张卡片,当按下下一个/上一个按钮时,播放器可以交换这些视图。作为一般规则,如果您有一个带有id的元素,那么您应该只使用一个视图实例渲染并绑定一次,否则您最终会遇到与现在相同的问题。

  • 你试图在FlashCardPlayer模型中添加太多内容。通常,模型应该只知道它们的数据,而不是用于显示它们的视图(部分原因是可能需要在各种视图中显示一个模型)。我不介意在模型上使用nextFlashCard()previousFlashCard()方法,因为这仍然存储有关集合的数据,但showCard()方法实际上正在移动到查看区域,因为它处理表示逻辑。更好的想法是让您的视图绑定到模型上的change:currentCardIndex事件,并使用this.model.get('currentCardIndex'))(或新{{}来处理新卡的显示1}}方法)来获得它。