2

我的目标是与lang该类User的所有实例共享类的配置属性。lang将保存一个翻译类的实例,该实例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();
}
4

2 回答 2

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();
      }
  },
  ....

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

于 2013-06-27T08:40:29.283 回答
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  }
于 2013-07-01T11:06:45.213 回答