[英]Javascript bind “this” to event
Below is an error I am getting from binding "this" to a click event in javascript. 以下是我从“ this”绑定到javascript中的click事件时遇到的错误。 The format of the js is Jaml/Mooml and may be unfamiliar to some but I assure you the syntax is proper. js的格式是Jaml / Mooml,可能对某些格式不熟悉,但我向您保证语法是正确的。 I have been binding "this" to many events in the same way and this is the first time I have seen this error so I am hoping some mootools, jaml or javascript expert will be able to derive a solution from the error and code below. 我以相同的方式将“ this”绑定到许多事件,这是我第一次看到此错误,因此我希望一些mootools,jaml或javascript专家能够从下面的错误和代码中得出解决方案。
Uncaught TypeError: Object [object Object] has no method 'get'
Mooml.engine.tags.div.Mooml.engine.tags.div.events.click:relay(a)
bubbleUpmootools-core-1.4.1.js:3948
delegation.addEvent.delegatormootools-core-1.4.1.js:4078
defn
here is the Jaml... 这是Jaml ...
'main': new Mooml.Template(null, function(data) {
div({'class': 'lists container'},
div({
'class': 'sources',
'events': {
'click:relay(a)': function(event) {
event.preventDefault();
new Resource({
'url': this.get('href'),
'method': 'FRIENDS',
'query' : {
'source': this.dataset.source
},
'onSuccess': function(response) {
console.log(response);
//this.renderTemplate('friends', response.mv, this);
}.bind(this),
'onFailure': this.onFailure
}).send();
}.bind(this)
}
Add var self = this
; 添加var self = this
; before your code and then use self
instead of this
in the callback functions. 在您的代码之前,然后在回调函数中使用self
代替this
。
Also have a look at http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/ and http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/ to learn more about how this
works. 也可以看看http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/和http://javascriptweblog.wordpress.com/2010/08/30/understanding- javascripts-this /了解更多有关this
工作原理的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.