2014-10-07 70 views
0

我試圖允許在表單上單擊一個按鈕,其票數爲+ = 1。你會認爲我可以做一些像idea.set({「votes」:+ = 1}),但它似乎並不那麼喜歡。我做了一個事件,聽取點擊我的upvote按鈕,現在我卡住了。誰能幫忙?將整數添加到骨幹模型屬性

IdeaVoter.Views.IdeasIndex = Backbone.View.extend(

    template: HandlebarsTemplates['ideas/index'], 
    initialize: function(){ 
    this.collection.on('reset',this.render, this) 
    this.collection.on('add',this.render, this); 
    }, 

    events: { 
    "submit #new_idea ": "createIdea", 
    "click #upvote": "upvote" 
     }, 

    render: function(){ 
    $(this.el).html(this.template()) 
    this.collection.each(this.addIdea) 
    this.collection.each(this.upvote) 
    return this; 
    }, 

    addIdea: function(idea){ 
    view = new IdeaVoter.Views.Idea({model: idea}) 
    $('#ideas').append(view.render().el) 
    }, 

    upvote:function(idea){ 
    idea.save() 
    } 


}); 

回答

0

我認爲你正在尋找一個簡單的

idea.set('votes', idea.get('votes') + 1); 

你可能想的方法添加到您的模型,隱藏背後的簡單idea.upvote()等。