backbone.js + JQuery: 'this' reference not accessible in JQuery-Event-Handler
I'm trying to use backbone.js in my webapp. I have a View which uses the JQuery draggable plugin to make a div draggable:
var ExampleView = Backbone.View.extend({
event开发者_运维知识库s: {
//...
},
initialize: function() {
_.bindAll(this, "render");
this.model.bind('change', this.render);
},
render: function() {
// icanhaz
this.el = ich.kinectdevtmpl();
$(this.el).draggable({
drag: function() {
alert(this.model);
}
});
return this;
}
});
But in the drag event handler I can't access 'this.model' because 'this' do not refer to the view anymore. So, how can I access my View in the JQuery event handler?
this has changed and is no longer what you want. you can assign this to a variable and use that instead.
var that = this;
$(this.el).draggable({
drag: function() {
alert(that.model);
}
});
精彩评论