Javascript 绑定“this”参加活动
下面是我将“this”绑定到 JavaScript 中的点击事件时遇到的错误。 js 的格式是 Jaml/Mooml,可能有些人不熟悉,但我向您保证语法是正确的。我一直以同样的方式将“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
这里是贾马尔...
'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)
}
Below is an error I am getting from binding "this" to a click event in javascript. The format of the js is Jaml/Mooml and may be unfamiliar to some but I assure you the syntax is proper. 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.
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...
'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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
var self = this
;在代码之前,然后在回调函数中使用self
而不是this
。另请查看 http://yehudakatz.com /2011/08/11/understanding-javascript-function-inspiration-and-this/ 和 http://javascriptweblog.wordpress.com/2010/08/30/understanding- javascripts-this/ 了解有关
this
工作原理的更多信息。Add
var self = this
; before your code and then useself
instead ofthis
in the callback functions.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.