我目前正在编写一个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函数。最好将其存储在模型中,还是可以在视图周围传递变量。
对此的任何帮助都会很棒!
由于
答案 0 :(得分: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);
}