2012-08-02 5 views
1

내 로컬 서버에서 JSON 데이터 파일을 읽으려면 Dojo와 Data/ItemFileWriteStore 모듈을 사용하고 있습니다. 내의 .js 그래서 ... 나는Dojo ItemFileWriteStore가 JSON 서버 파일을 읽지 않습니다.

var myDataStore = new ItemFileWriteStore({ 
    url: "app/data/mydata.json", 
    handleAs: "json", 
    clearOnClose: true, 
    urlPreventCache: true 
}) 

이 나의 복귀 선언 함수의 postCreate 함수에 위치를 파일 :

define([ 
    "dojo/_base/declare", 
    "com/cayuse/base/_widget", 
    "dojo/text!./templates/myform.html", 
    ...  
    "dojo/data/ItemFileWriteStore", 
    "dojo/store/DataStore", 
    "dojo/store/Observable", 
    "dojo/data/ObjectStore", 
    "dojo/domReady!" 
    ], 
    function(declare, widget, template, ..., ItemFileWriteStore, DataStore, 
     Observable, ObjectStore){ 
     return declare("app.myform", widget, { 
      templateString: template, 

      postCreate: function(){ 

       domConstruct.create("link",{ 
        type: "text/css", 
        rel: "stylesheet", 
        href: require.toUrl('dojox/form/resources/CheckedMultiSelect.css') 
       }, document.getElementsByTagName("head")[0]); 

       // data store 
       var myDataStore = new ItemFileWriteStore({ 
        url: "app/data/mydata.json", 
        handleAs: "json", 
        clearOnClose: true, 
        urlPreventCache: true 
       }) 
       console.log(myDataStore); 
      } 
     }); 
    } 
); 

나는 위에서 보는 것과 데이터 저장소 액세스를 변경할 수 있습니다 IFWS 방법을 사용하여

var myDataStore = dojo.xhrGet({ 
    url: "app/data/mydata.json", 
    handleAs: "json", 
    load: function(data, ioArgs){ 
     console.log(data); 
    } 
}); 

으로 설정하고 아무 문제없이 파일을 찾습니다.

이것은 너무 기괴합니다! 여기서 무엇이 잘못 될지에 대한 아이디어가 있습니까?

업데이트 : 여기는 내가 읽은 파일의 데이터입니다. 나는 그것이 JSON 형식을 따르는 것으로 믿는다. 그렇지 않으면 알려주세요. xhr 잘 읽습니다.

{ "identifier": "id", 
    "label": "firstName", 
    "items":[ 
    {"id":"0","firstName":"Robert","website":"www.barker.com","email":"[email protected]","bday":"1928-08-09","color":"Blue","toolkits":["Dojo","Moo"],"sendEmail":["on"],"format":"HTML"}, 
    {"id":"1","firstName":"Vanna","website":"www.white.com","email":"[email protected]","bday":"1968-07-23","color":"Green","toolkits":["Dojo","jQuery"],"sendEmail":["off"],"format":"Text"} 
    ] 
} 

답변

2

는 ItemFileWriteStore는 데이터가 이런 식으로 구성되고이 필요합니다

모든 당신의 "라벨"필드 인 당신의 "ID"필드, '라벨'인 '식별자'입니다
{ identifier: 'abbr', 
    label: 'name', 
    items: [ 
    { abbr:'ec', name:'Ecuador',   capital:'Quito' }, 
    { abbr:'eg', name:'Egypt',    capital:'Cairo' }, 
    { abbr:'sv', name:'El Salvador',  capital:'San Salvador' }, 
    { abbr:'gq', name:'Equatorial Guinea', capital:'Malabo' }, 
    { abbr:'er', name:'Eritrea',   capital:'Asmara' }, 
    { abbr:'ee', name:'Estonia',   capital:'Tallinn' }, 
    { abbr:'et', name:'Ethiopia',   capital:'Addis Ababa' } 
]} 

당신의 "items"라는 배열 안에있는 객체.

여기에서 확인할 수 있습니다. in ItemFileWriteStore's documentation. 이와 같이 구조화 된 JSON 데이터가없는 경우 파일을 IFWS로 읽고 실제로 데이터를 읽지 않을 수도 있습니다.

dojo 1.7에는 이러한 구조가 필요없는 다른 상점 구현이 있습니다 (예 : Memory 다른 파일 읽기 기술과 결합하여 동일한 결과를 얻을 수 있도록 저장하십시오.

+0

응답 eburgos에 감사드립니다. 파일 데이터를 게시 했어야합니다. 그것은 그 구조를 준수합니다. 나는 그것을 나의 지위에 추가 할 것이다. 한 가지 사실은이 포럼 상태에서 속성과 값 모두 진정한 JSON 형식으로 인용해야한다는 것입니다. 두 가지 방법을 시도했지만 문제는 파일을 가져 오지 못한다는 것입니다. 나는 Net XHR 활동에서 Firebug를보고 파일에 손대지 않는다. 게다가 dojo xhr을 사용할 때 같은 파일을 잘 읽습니다. 정말 이상 하네, 내가 가진이 문제 야. – teaman

+0

그래서 Firebug Net XHR 활동이 요청을하지 않습니까? – eburgos

0

dojo/data/ItemFileWriteStore 대신 json 데이터 파일을 읽으려면 dojo.data.ItemFileReadStore를 사용해보십시오.

dojo.data.ItemFileWriteStore는 json 데이터를 기록하는 데 사용됩니다.

+0

ItemFileReadStore의 기능이 ItemFileWriteStore에 포함되어 있다고 생각했습니다. 당신이 정확하고 차이가 있다면 IFWS의 하위 집합을 가진 객체가 IFWS 자체는 아니지만 작동한다는 것은 이상 할 것입니다. – teaman

0

위에서 코드를 게시 할 때 코드가 정확하면 인터프리터는 ItemFileWriteStore 지정에서 세미콜론을 생략했다는 사실을 좋아하지 않을 수 있습니다. ';'을 추가하십시오. 아래와 같이 :

  // data store 
      var myDataStore = new ItemFileWriteStore({ 
       url: "app/data/mydata.json", 
       handleAs: "json", 
       clearOnClose: true, 
       urlPreventCache: true 
      }); 
      console.log(myDataStore); 
관련 문제