2013-05-05 1 views
2

callParent()으로 처리하기 전에 Json Store를 준비하려고하는데 오류가 발생합니다.ExtJS 4 - callParent에 대한 비동기 콜백이 예외를 throw합니다.

그러나 me.callParent()은 비동기 콜백없이 잘 작동합니다.

Ext.define('My.desktop.AppExt', { 
    extend: 'Ext.ux.desktop.App', 

    someStore: null, 

    init: function() { 
     var me = this; 
     me.someStore = Ext.create('My.store.SomeStore'); 
     me.someStore.load({ 
      scope: this, 
      url: 'some/json/url', 
      callback: function(records, opt, success) { 
       if (success) { 
        me.callParent(); // BOOM! ERROR HERE 
       } 
      } 
     }); 
    } 
}); 

ERROR :

라인 4245에서

되지 않은 예외 //localhost/js/ext-all-debug.js

0x800a138f에서 칼럼 17 - 자바 스크립트 런타임 에러 :

정의되지 않은 null 참조의 속성 '수퍼 클래스'를 가져올 수 없습니다.

답변

5

callParent는 당신이 실제로 서브 클래스 메소드로부터 "직접"를 호출하지 않는, 그래서 만약 올바른 방법, 수동으로 호출해야합니다 : 내가 아는 방법

Ext.define('A', { 
    foo: function(){ 
     console.log('foo', 'a');  
    } 
}); 

Ext.define('B', { 
    extend: 'A', 
    bar: function(){ 
     this.self.superclass.foo.call(this);  
    } 
}); 

Ext.onReady(function(){ 
    var o = new B(); 
    o.bar(); 
}); 
+0

는 빠른 응답 주셔서 감사합니다! callParent()를 제외하고는 비동기 호출에 대해 알고 있어야하는 동일한 상황에서 유사한 다른 함수가 있습니까? – Tom

+1

'callParent'는 호출해야하는 것을 알아 내기 위해 호출하는 함수를 질의합니다. 따라서 "마법"호출처럼 보이는 모든 것은 동일한 기술을 사용합니다. –

0

하나는 추가 매개 변수를 사용하는 것입니다, 그 표시 그 부모 메소드가 호출되어야한다 : 당신이 this.self.superclass.init.call (이)를 사용하는 경우 클래스에 대해 누군가가 아이를 작성합니다까지만

init: function(callParent) { 
    if (callParent) { 
     this.callParent(); 
    } 
    var me = this; 
    me.someStore = Ext.create('My.store.SomeStore'); 
    me.someStore.load({ 
     scope: this, 
     url: 'some/json/url', 
     callback: function(records, opt, success) { 
      if (success) { 
       this.init(true); 
      } 
     } 
    }); 
} 

그것이 확인 될 것입니다. this.self.superclass는 인스턴스의 수퍼 클래스를 가리 키므로 Ext.ux.desktop.App 대신 My.desktop.AppExt를 가리 킵니다.

업데이트 날짜 : 24.08.2016 : 더 똑똑한 솔루션 게시 (다른 답변보기).

0

이 목적을 위해 가장 좋은 방법은 callParent에서() 함수와 같은, 그러나 그것을 호출하지 않고 parentMethod 링크를 얻을 수 있습니다 :

/** 
* Returns reference to the parent class method. Works as {@link Ext.Base#callParent}, but doesn't invoke the 
* method. 
* @return {Function} Parent class method. 
*/ 
getParentMethod: function() { 
    var method, 
     superMethod = (method = this.getParentMethod.caller) && (method.$previous || 
      ((method = method.$owner ? method : method.caller) && method.$owner.superclass[method.$name])); 
    return superMethod; 
}, 


sampleMethod: function() { 
    var parentMethod = this.getParentMethod(); 
    var parentArguments = arguments; 
    someAsyncFunc(function() { 
     parentMethod.apply(this, parentArguments); // calls parent.sampleMethod(...) 
    }, this); 
} 
관련 문제