Extjs与所有实例共享类配置

时间:2013-06-27 08:29:13

标签: extjs extjs4.1

我的目标是与此类的所有实例共享类lang的配置属性Userlang将保留转换类Language的实例,该实例将一次异步加载数据。

问题是我无法在配置中获取回调以调用实例的函数。

Ext.define( 'X.User.controller.Main', {
  extend: 'Deft.mvc.ViewController',

  config: {
     // create one Language instance once for all User instances
     lang: Ext.create( 'X.Language.Main', { 
        loaded: false,
        language: 'de', 
        // this callback gets called correctly after loading language resources
        callback: function() { 
           // ERROR since "this" is window, not the User instance
           this.lang.loaded = true; // should mark it loaded for next instances
           this.start(); // should start the instance logic
        }
     } )
  },

  init: function() {
     var that = this;
     if( that.lang && that.lang.loaded )
     {
        that.start();
     }
  },

  start: function() {
     // use the translation functions...
     oView.setLoading( that.lang.get( 'checkingPrivileges' ) );
     ...
  }

我怎样才能让它发挥作用?这种情况有更好的设计吗?


回调调用:

constructor: function( oConfig ) {
     var that = this;
     that.getLanguageFile( oConfig );
     that.initConfig( oConfig );
}

getLanguageFile: function( oConfig ) {
   var fCallback = oConfig.callback || Ext.emptyFn;
   ...
   // in my ajax success function...
      fCallback();
}

2 个答案:

答案 0 :(得分:0)

判断“这是窗口”你的回调函数调用方式是错误的。这里没有代码,但似乎你直接调用它callback(arg),要使用上下文进行调用,你应该使用像callback.call(scope, arg)这样的范围

问题是您在X.Language.Main之前启动的X.Language.Main实例,这就是您没有范围的原因。在以下之后做到:

Ext.define( 'X.User.controller.Main', {
  extend: 'Deft.mvc.ViewController',         

  init: function() {
      var lang = Ext.create( 'X.Language.Main', { 
        loaded: false,
        language: 'de', 
        callback: function() { 
           this.lang.loaded = true; // should mark it loaded for next instances
           this.start(); // should start the instance logic
        },
        scope: this
      });

      this.config = {lang: lang};

      var that = this;
      if( that.lang && that.lang.loaded ){
        that.start();
      }
  },
  ....

然后用范围调用它或使用绑定。

答案 1 :(得分:0)

我目前的解决方案是:

Ext.define( 'X.plugins.language.Main', {
  config: {
     loaded: null,
     plugin: null,
     translations: null,
     listening: false
  },
  constructor: function( oConfig ) {
     this.initConfig( oConfig );
  },
  ...

所有其他课程如下:

Ext.define( 'X.plugins.usermanagement.controller.Main', {     
  config: {
     language: Ext.create( 'X.plugins.language.Main', {
        plugin: 'usermanagement'
     } )
  },

  init: function() {
     this.language.init( {
        callback: Ext.bind( that.start, that )
     } );
  },
  start: function() { // now start the plugin  }