2012-06-12 3 views
19

필자는 Ext JS 4에서 무엇이 필요한지 알아 내려고 노력해 왔으며 합리적인 답을 찾지 못했습니다.Ext JS 4 : 요점은 무엇입니까?

Ext.Loader.setConfig({ 
    enabled: true 
}); 

Ext.Loader.setPath('Ext.ux', 'examples/ux'); 

Ext.application({ 
    name: 'Test', 
    appFolder: 'app', 
    controllers: ['TheController'], 
    requires: ['Test.Utils', 'Test.Utils2'], // I don't think this does anything... couldn't find this option for Ext.application 
    launch: function() { 
    Ext.create('Ext.Viewport', { 
     layout: 'border', 
     items: [{ 
     xtype: 'thegrid', 
     region: 'center', 
     title: 'blah!' 
     }] 
    }); 
    } 
}); 

응용 프로그램/컨트롤러/TheController.js

Ext.define('Test.controller.TheController', { 
    extend: 'Ext.app.Controller', 
    models: ['TheModel'], 
    stores: ['TheStore'], 
    views: ['TheGrid'], 
    init: function() { 
    } 
}); 

응용 프로그램 /보기/TheGrid.js

을 app.js : 이제 나는 다음과 같은 코드가 있다고 가정 해 봅시다
Ext.define('Test.view.TheGrid', { 
    extend: 'Ext.grid.Panel', 
    alias: 'widget.thegrid', 
    requires: ['Test.store.TheStore'], 
    store: 'TheStore', 
    columns: [ 
    {header: 'Name', dataIndex: 'name'}, 
    {header: 'Phone', dataIndex: 'phone'}, 
    {header: 'Hello', dataIndex: 'hello'} 
    ] 
}); 

응용 프로그램/저장/TheStore.js

Ext.define('Test.store.TheStore', { 
    extend: 'Ext.data.Store', 
    requires: ['Test.model.TheModel', 'Test.Utils'], 
    model: 'Test.model.TheModel', 
    data: [ 
    {name: 'keanu reeves', phone: '1800matrices', hello: Test.Utils.getText()}, 
    {name: 'james earl jones', phone: '1800starwar', hello: 'nothing here'}, 
    {name: 'barack obama', phone: '1800prsidnt', hello: 'hello world'} 
    ] 
}); 

응용 프로그램/모델/TheModel.js

Ext.define('Test.model.TheModel', { 
    extend: 'Ext.data.Model', 
    fields: [ 
    {name: 'name', type: 'string'}, 
    {name: 'phone', type: 'string'}, 
    {name: 'hello', type: 'string'} 
    ] 
}); 

응용 프로그램/Utils.js

Ext.define('Test.Utils', { 
    singleton: true, 
    requires: ['Test.Utils2'], 
    getText: function() { 
    return Test.Utils2.hello + 'world'; 
    } 
}); 

응용 프로그램 /Utils2.js

Ext.define('Test.Utils2', { 
    singleton: true, 
    hello: 'hello' 
}); 

나는 이것이 실제로 긴 예제라는 것을 알고 있지만, 나는 내가하고있는 것을 완전히 묘사해야했다. Utils는 Utils2의 hello 변수를 호출해야하기 때문에 Utils2를 사용합니다. 나머지 코드는 그리드를 설정하고 TheStore에서 Utils.getText 함수를 호출하는 것입니다. Firebug는 TheStore.js의 라인 6에서 Test.Utils is undefined을 던졌습니다. 그 시점에서 Test.Utils는 분명히 존재하지 않지만 Test.Utils2는 존재합니다.

내 질문은 ... 왜 Utils2가 존재하지만 Utils는 그렇지 않습니까? 나는 내가 필요로하는 수업을 가져와야한다고 생각했기 때문에 그들을 사용할 수있게되었지만 나는 틀렸다고 생각한다. 나는 Sencha 워드 프로세서와 수많은 스레드를 읽었지만 실제로는 아무 것도 이해하지 못했고 실제로이 예제를 설명하지는 않습니다. 누구나 여기 통찰력을 발휘할 수 있습니까? 나는 그것을 고맙게 생각할 것이다.

** 또한 나는 여기서 몇 가지 바보 같은 일을하고 있음을 알고 있지만, 단지 예시 일 뿐이므로 글로벌 Utils를 결합하거나 전역을 사용하지 않을 것입니다 ... 나는 단지 요구 옵션을 찾아 내려고. 아래 Izhaki의 대답에

UPDATE

덕분에, 내가 뭔가를 알아 냈어. 내가 정의하고있는 클래스에서 필요한 클래스를 사용하려면 객체가 만들어지기를 기다려야합니다 (IE, initComponent 사용).

app/저장/TheStore.js

Ext.define('Test.store.TheStore', { 
    extend: 'Ext.data.Store', 
    requires: ['Test.model.TheModel'], 
    model: 'Test.model.TheModel' 
}); 

응용 프로그램 /보기/TheGrid.js

Ext.define('Test.view.TheGrid', { 
    extend: 'Ext.grid.Panel', 
    alias: 'widget.thegrid', 
    requires: ['Test.store.TheStore'], 
    store: 'TheStore', 
    columns: [ 
    {header: 'Name', dataIndex: 'name'}, 
    {header: 'Phone', dataIndex: 'phone'}, 
    {header: 'Hello', dataIndex: 'hello'} 
    ], 
    // This is the change that works 
    initComponent: function() { 
    this.callParent(); 
    this.store.loadData([ 
     {name: 'keanu reeves', phone: '1800matrices', hello: Test.Utils.getText()}, 
     {name: 'james earl jones', phone: '1800starwar', hello: 'nothing here'}, 
     {name: 'barack obama', phone: '1800prsidnt', hello: 'hello world'} 
    ]); 
    } 
}); 

이 작동하지만, 내 마지막 질문은 ...TheStore의 TheModel 및 TheGrid의 TheStore에 대한 요구 사항을 가져야합니까? TheController가 TheGrid에서 Test.Utils를 사용할 수 있기 때문에 TheController가 필요로하는 모든 것을 처리하고있는 것처럼 보이지만 TheGrid는 Test.Utils이 필요하다고 명시하지 않습니다.

또한 Sencha docs의 this example도 TheStore가 만들어 질 때까지 Test.Utils를 사용하지 않았기 때문에 Sencha docs의 더 혼란 스럽습니다.하지만이 예제는 초기화를 기다릴 필요없이 Child 클래스를 사용할 수있는 것 같습니다. initComponent 사용).

답변

13

사실 이것은 전혀 바보 같은 질문이 아닙니다.

는 "이 클래스의 객체를 생성 할 때, 동적으로 먼저 필요한 스크립트를로드 할 수 있는지 확인하십시오"당신은 볼 수

는 ExtJS로 말할 수있는 방법으로해야합니다.

는이 라인에 대한 권리입니다

requires: ['Test.Utils', 'Test.Utils2'], 
app.js 필요하지

, 이유 존재는 응용 프로그램이 이미 가지고 있다는 것입니다 : 말과 동일합니다

controllers: ['TheController'], 

당신은 JS가 필요 TheController이 상주하는 스크립트 (모든 모델/뷰/컨트롤러/저장소 정의는 관련된 스크립트가 필요하다는 것을 의미합니다. 즉, 동적으로로드됩니다).

TheController이있다 :이 동적으로로드

requires: ['Test.model.TheModel', 'Test.Utils'], 

이 - 같은 requiresapp.js을 필요하지 않은 이유는; 구성되어 TheStore까지 범위에는 Test.Utils가 없습니다 -

당신이 방화범이 정의되지 Test.Utils을 던지고받을 이유는 아직 동적으로로드되지 않은 객체에 대한 참조와 설정 (hello)를 제공한다는 것입니다. 이

  • 그것은 JSBuilder 포함 할 파일을 알고 도움없이

  • +0

    나는이 대답을 좋아하지만, 설명 할 수없는 몇 가지 사항이 남아있다. – incutonez

    +0

    첫 번째 새로운 질문 : TheStore에서 TheModel을 요구하지 않아도됩니다. (모델 구성 에서처럼 모델 구성에서와 같이 필요하지만 설정자와 getter를 필요로하지 않습니다.). TheGrid에서 요구 사항을 필요로하지 않아도됩니다. 컨트롤러가 모든 것을 처리하고 있습니다. – Izhaki

    +0

    기본적으로 응용 프로그램이 시작되면 모든 컨트롤러와 해당 구성 객체에서 참조되는 모든 모델/뷰/저장소의 인스턴스가 동적으로로드되고 만들어집니다. 일단 이것이 끝나면, 이것들 모두는 뷰 (view xtypes 포함)의 범위에 들어갑니다. – Izhaki

    0
    1. hasMany의 관계는 단순히 작동하지 않습니다. 예를 들어, 뷰포트가 경계선 레이아웃을 사용하는 경우 잘못 포함되며 사용 또는 필요성을 포함시켜야합니다.