2013-02-05 92 views
0

我目前正在編寫一個骨幹應用程序,並已陷入似乎是一個非常簡單的問題,但似乎無法解決。如何傳遞一個變量圍繞視圖與骨幹

比方說,我有這樣的(演示代碼)視圖

var View = Backbone.View.extend({ 
    initialize: function() { 
     console.log('created a view'); 
    }, 
    events: { 
     'click button':'buttonClicked', 
     'click link':'linkClicked' 
    }, 
    buttonClicked: function (event) { 
     // I would like to store the event and pass this to the 
     // linkClicked function below when the linkClicked function is called 
     var $currentEvent = $(event.currentTarget); 
    }, 
    linkClicked: function() { 
     // When the link is clicked I would like to gain access to $currentEvent 
    } 
}); 

我想通過單擊鏈接時變量$ currentEvent的功能linkClicked。將模型存儲在模型中還是可以將變量傳遞給視圖最好?

任何幫助,這將是偉大的!

感謝

回答

1

你可以做這樣的事情:

initialize: function() { 
    /** Initialize the variable when View is first created. */ 
    this.currentEvent = 'something'; 
}, 
buttonClicked: function (event) { 
    /** Assign variable when button is clicked. */ 
    this.currentEvent = $(event.currentTarget); 
}, 
linkClicked: function() { 
    /** Receive the value of the variable and output it. */ 
    console.log(this.currentEvent); 
} 
+0

驚豔...感謝您的快速響應。沒有意識到這很簡單,但完全合理。再次感謝 – darylhedley