[英]How to stay in the this context during an ajax call (jquery + coffescript)
I made a class : 我做了一堂课:
class @GameMap
options : {
'width' : 100,
'height' : 100,
'tileWidth' : 45,
'tileHeight' : 20,
'scale' : 5,
'moveDistance' : 100, #Distance to move with the map controlers
'showErrorMessages' : true,
'zindexDecorElementBase' : 99999,
'zindexGroudElementBase' : 88888
}
lang : {
'errContainerMissing' : "L'élement pour créer la carte n'existe pas"
}
constructor: (@element) ->
this.run()
run: ->
this.loadDatas()
loadDatas: (top,left) ->
$.ajax(
url: "/map/getnewcoord",
data: {
map_width : this.options.width,
map_height : this.options.height,
tile_height : this.options.tileHeight,
tile_width : this.options.tileWidth,
window_large : $('.map-windows').width(),
window_height: $('.map-windows').height(),
top : top ,
left : left
},
success: (data) ->
$.each(data,(index,item) ->
this.createTile(item.posX,item.posY);
)
)
createTile: (m,n) ->
#We are in the tile reference
yDelta = Math.round((options.width ) * options.tileHeight );
console.log('pass')
$ ->
gamemap = new GameMap("#map .block-map")
But i got the error 但我得到了错误
this.createTile is not a function
this.createTile不是一个函数
It's because the "this" is not the "this" of my class but one of item return by my json call. 这是因为“this”不是我班级的“this”,而是我的json调用返回的项目之一。
How can i do to keep the "this" of my class in the ajax success callback function? 如何在ajax成功回调函数中保持我的班级“this”?
Keep a reference to this
. 请参考
this
。
A lot of people use a pattern such as var that = this
(or self
if it reads better for you). 很多人都使用
var that = this
的模式(如果它对你来说更好的话,那就是self
)。
Then, in your inner function, replace references to this
with that
, which will be the parent function's this
. 然后,在你的内部函数,替换引用
this
与that
,这将是父函数的this
。
Alternatively, you can wrap the anonymous function definitions with $.proxy()
... 或者,您可以使用
$.proxy()
包装匿名函数定义...
fn = $.proxy(fn, this);
...which will ensure the this
inside is always the parent's this
. ...这将确保
this
里面始终是家长的this
。 $.proxy()
is a cross browser way of achieving bind()
(which isn't implemented in the older IEs or Safari). $.proxy()
是一种实现bind()
的跨浏览器方式bind()
在旧的IE或Safari中没有实现)。
Here's the CoffeeScript way to do it: 这是CoffeeScript的方法:
success: (data) =>
$.each(data,(index,item) =>
this.createTile(item.posX,item.posY);
)
This compiles to something very similar to alex's answer, but is, I think, much more readable. 这编译成与alex的答案非常相似的东西,但我认为,它更具可读性。 The
=>
operator defines a function while using the this
that was present when it was defined, rather than when it was called. 的
=>
操作者在使用定义了一个函数this
是存在当它被限定,而不是当它被调用。
While we're at it, you may find it more readable to use @
instead of this
: 虽然我们在这,你可能会发现它更具有可读性用
@
代替this
:
@createTile(item.posX,item.posY);
You can pass the context
as param to the ajax()
method: 您可以将
context
作为参数传递给ajax()
方法:
$.ajax({ /*...,*/ context: this/*, ...*/ });
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.