0

我目前正在编写一个 Backbone 应用程序,并且陷入了一个看似非常简单但似乎无法解决的问题。

假设我有这样的视图(演示代码)

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 。最好将它存储在模型中,还是我可以在视图周围传递变量。

对此的任何帮助都会很棒!

谢谢

4

1 回答 1

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);
}
于 2013-02-05T23:11:20.147 回答