Extjs4几个小知识点

1、Why user "var me=this" in Extjs4?
有个英文解释很好:

Say you have a method in your object A which itself makes an ajax request which has a callback. In this callback you want to use a property "blob" of your original object A. You can not use "this.blob" in this callback because "this" does not refer to your object A anymore (Your scope is in the ajax request). For this reason you save "this" to a variable "me" before you call the function and then refer to "me.blob" to reference the porperty A.blob.

归根结底就是作用域的原因,就是如果你在当前对象中的某一个函数用this的时候this可能不是指代当前的对象了,你要用var me=this;这句话把当前对象的指针保存下来就可以用了。
举例:

// a grid, for example
initComponent: function() {
  var me = this;
  ...
  me.toolbar = new Ext.Toolbar({
    items: [
      { text: 'refresh', handler: function() { me.store.reload(); } }
    ]
  });
inside the handler, me references the grid, while "this" references the button. 
Surely a source of confusion for many a programmer at some point.
如果在函数里面写了var me=this;这句话,this指代的是直接调用这个函数的对象,所以在handler函数中用this的话是指代的button而不是grid。因为handler是数据button的。

2、Ext.Ajax.request()
Ext.Ajax.request({
    url: 'ajax_demo/sample.json',
    success: function(response, opts) {
        var obj = Ext.decode(response.responseText);
        console.dir(obj);
    },
    failure: function(response, opts) {
        console.log('server-side failure with status code ' + response.status);
    }
});

非常重要的方法。

3、注意store.loadData();store.loadRecords();的使用。

4、 constructor: function (config) {
        Ext.apply(this, config);
}
这是一个构造函数,就是在你利用基类构造对象时把你所有的初始化的参数、属性赋给新建的对象。

posted @ 2013-07-26 12:52  楠楠IT  阅读(285)  评论(0编辑  收藏  举报