2013-06-12 5 views
0

저는 Sencha Touch로 시작하여 2.2.1 버전에서 작업하고 있습니다. 웬일인지, 나는 나의 json에게 올바르게 해석 할 수 없다. 내 크롬 개발자 도구에서 json을 볼 수 있기 때문에 응답 문제가 아님을 알고 있습니다.로컬 json을 구문 분석 할 수 없습니다.

여기 여기 내 store

Ext.define('MyApp.store.Tasks', { 
    extend: 'Ext.data.Store', 
    requires: [ 
     'MyApp.model.Task' 
    ], 

    config: { 
     autoLoad: true, 
     storeId: 'tasksStore', 
     model: 'MyApp.model.Task', 
     proxy: { 
      type: 'ajax', 
      url: 'tasks.json', 
      reader: { 
       type: 'json', 
          rootProperty: 'tasks' 
      } 
     } 

    } 
}); 

입니다 내 상점을 테스트하는 Jasmine을 사용하고 내 Model

Ext.define('MyApp.model.Task', { 
    extend: 'Ext.data.Model', 

    config: { 
     fields: [ 
      { name: 'id', type: 'int' }, 
      { name: 'task', type: 'string', defaultValue: 'task' } 
     ] 
    } 
}); 

입니다. 내 사양은 여기

describe('MyApp.store.Tasks', function() { 
    it('Number of tasks should be four', function() { 
    var store = Ext.create('MyApp.store.Tasks'); 

    expect(store.getCount()).toBe(4); 

    }); 
}); 

그리고 여기 내 샘플 json 파일이 있습니다. 그것은 Sencha의 index.html 파일과 같은 디렉토리에 있으며 루트 디렉토리입니다.

{ 
    "tasks":[ 
     { 
     "task":"Some Product", 
     "id":0 
     }, 
     { 
     "task":"Another Product", 
     "id":1 
     }, 
     { 
     "task":"A third product", 
     "id":2 
     }, 
     { 
     "task":"A fourth product", 
     "id":3 
     } 
    ] 
} 

인스턴스화 문제 때문입니까? 아니면 여기서 중요한 부분을 놓치고 있습니까? 프록시 유형에 대해 jsonp를 시도했지만 응답 주위에 래퍼가 필요하며이를 수행하는 방법을 잘 모릅니다. Safari와 Chrome에서 모두 테스트 중이지만 불행히도 두 브라우저 모두에서 단위 테스트가 실패합니다.

감사합니다.

답변

1

저장소로드는 비동기식이므로 저장소를 만든 직후 데이터에 액세스 할 수 없습니다. 이제

var store = Ext.create('MyApp.store.Tasks', {autoLoad: false}); 

store.load({ 
    callback: function(records, operation, success) { 
     if (success) { 
      // here the store has been loaded 
      expect(store.getCount()).toBe(4); 
     } 
    } 
}); 

: 당신은 또한 load method에 콜백을 전달할 수 있습니다

var store = Ext.create('MyApp.store.Tasks'); 

// you cannot use the store's data yet 
// expect(store.getCount()).toBe(4); 

store.on('load', function(records, success) { 
    if (success) { 
     // here the store has been loaded 
     expect(store.getCount()).toBe(4); 
    } 
}); 

또는 : 저장소가로드 된 경우

는 상점의 load event에들을 수 있습니다 알고 즉, make your Jasmine test asynchronous이어야합니다.

describe('MyApp.store.Tasks', function() { 
    it('Number of tasks should be four', function() { 
     var result = null, 
      store = Ext.create('MyApp.store.Tasks'); 

     store.on('load', function(store, records, success) { 
      result = success; 
     }); 

     // using jasmine async... 
     waitsFor(function() { 
      return result !== null; 
     }); 

     // this functin will be executed when the one in waitsFor returns true 
     runs(function() { 
      expect(store.getCount()).toBe(4); 
     }); 
    }); 
}); 
+0

매우 좋음을 의미합니다. 설명! 고맙습니다! – javaCity

관련 문제