Ext scope 学习

//http://www.cnblogs.com/yin-jingyu/archive/2011/08/03/2126460.html
Ext scope 学习
  首先,用一句话来概括scope的作用:scope就是用来解决 js 中 this 的指向问题。

  下面进行讨论:

   1、 关于JavaScript中this的使用,这是一个由来已久的问题了。我们这里就不介绍它的发展历史了,只结合具体的例子,告诉大家可能会遇到什么问题,在遇到这些问题时EXT是如何解决的。在使用EXT时,最常碰到的就是使用Ajax回调函数时出现的问题,如下面的代码所示。

<input type="text" name="text" id="text"><input type="button" name="button" id="button" value="button">


现在的HTML 页面中有一个text输入框和一个按钮。我们希望按下这个按钮之后,能用Ajax去后台读取数据,然后把后台响应的数据放到text中,实现过程如代码清单10-6所示。

代码清单10-6 Ajax中使用回调函数

function doSuccess(response) {text.dom.value = response.responseText;}

Ext.onReady(function(){
Ext.get('button').on('click', function(){
var text = Ext.get('text');
Ext.lib.Ajax.request(
'POST',
'08.txt',
{success:doSuccess},
'param=' + encodeURIComponent(text.dom.value)
);
});
});


在上面的代码中,Ajax已经用Ext.get('text')获得了text,以为后面可以直接使用,没想到回调函数success不会按照你写的顺序去执行。当然,也不会像你所想的那样使用局部变量text。实际上,如果什么都不做,仅仅只是使用回调函数,你不得不再次使用Ext.get('text')重新获得元素,否则浏览器就会报text未定义的错误。

在此使用Ext.get('text')重新获取对象还比较简单,在有些情况下不容易获得需要处理的对象,我们要在发送Ajax请求之前获取回调函数中需要操作的对象,有两种方法可供选择:scope和createDelegate。

为Ajax设置scope。

function doSuccess(response) {this.dom.value = response.responseText;}Ext.lib.Ajax.request('POST','08.txt',{success:doSuccess,scope:text},'param=' + encodeURIComponent(text.dom.value));


在Ajax的callback参数部分添加一个scope:text,把回调函数的scope指向text,它的作用就是把doSuccess函数里的this指向text对象。然后再把doSuccess里改成this.dom. value,这样就可以了。如果想再次在回调函数里用某个对象,必须配上scope,这样就能在回调函数中使用this对它进行操作了。

为success添加createDelegate()。

function doSuccess(response) {this.dom.value = response.responseText;}

Ext.lib.Ajax.request(
'POST',
'08.txt',
{success:doSuccess.createDelegate(text)},
'param=' + encodeURIComponent(text.dom.value)
);


createDelegate只能在function上调用,它把函数里的this强行指向我们需要的对象,然后我们就可以在回调函数doSuccess里直接通过this来引用createDelegate()中指定的这个对象了。它可以作为解决this问题的一个备选方案。

如果让我选择,我会尽量选择scope,因为createDelegate是要对原来的函数进行封装,重新生成function对象。简单环境下,scope就够用了,倒是createDelegate还有其他功能,比如修改调用参数等。


2、再通过几个简单的例子来解释:

code is below:

  

<html>
<head>
<script>
var p1 = {
name: 'p1',
fn: function() {
var scope = this.scope? this.scope:this;
alert(scope.name)
}
};
var p3 = {
name: 'p3'
};
var p2 = {
name: 'p2',
//scope: p1, // assign scope
fn: p1.fn // copying p1.fn loses its scope
};
p1.fn();
p2.fn();
</script>
</head>
</html>
Running that code will pop up two times: 'p1' and 'p2' ... even though p2's fn uses the fn from p1 but since it is run within p2 scope, it
displays p2's name.
Uncomment the scope config in p2 and refresh. Now the page will pop up 'p1' two times.
Set the scope config option to point to p3 and refresh. Now the page will pop up 'p1' and 'p3'.
All the above behaviors are made possible since the p1.fn function takes the scope into consideration. Ext, on the other hand, uses a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值