2012-06-29 71 views
1

此代碼有什麼問題?我可以得到我的json並用警報進行調試(以便部分可以與xhr一起使用)...例如,如果我在函數(xhr),alert(data [0] .name)中執行此操作,我會得到正確的值。網上沒有太多的例子...但指定列和添加對象存儲沒有顯示任何東西...基本上,我只是想加載一些json文件(本地)並將其呈現在網格上,但我最終將使用REST處理我的應用程序中的CRUD(因此,我將在不久的將來使用JsonRest)。Dojo OnDemandGrid(dgrid)與ObjectStore(本地json)

我認爲它也必須與AJAX ...我應該把同步到真正的(因爲它似乎我的全局變量doesn,噸工作正常...未定義)。

define([ 
    "dojo/_base/declare", 
    "dijit/_WidgetBase", 
    "dgrid/OnDemandGrid", 
    "dojo/_base/xhr", 
    "dojo/store/Memory", 
    "dojo/data/ObjectStore", 
    "dgrid/Keyboard", 
    "dgrid/Selection" 
], function(
    declare, 
    _WidgetBase, 
    Grid, 
    xhr, 
    Memory, 
    ObjectStore, 
    Keyboard, 
    Selection 
){ 
    var dataStore; 

    xhr.get({ 
     url: "app/resources/data/content.json", 
     handleAs: "json" 
    }).then(function(data){ 
    dataStore = new ObjectStore({ objectStore:new Memory({ data: data.items }) }); 
    }); 

    return declare([_WidgetBase, Grid, Keyboard, Selection], { 
    store: dataStore, 
    columns:{ 
      name: { label: "name" }, 
      autodelete: { label: "autodelete" }, 
      groupe_id: { label: "groupe_id" }, 
      global: { label: "global" }, 
      date: { label: "date" }, 
      duree: { label: "duree" }, 
      description: { label: "description" }, 
      fichier: { label: "fichier" }, 
      pleinecran: { label: "pleinecran" }, 
      repertoire: { label: "repertoire" }, 
      taille: { label: "taille" }, 
      expiration: { label: "expiration" }, 
      id: { label: "id" }, 
      catergorie: { label: "catergorie" }, 
      brouillon: { label: "brouillon" } 
     }, 

    postCreate: function() { 
    } 
}); 
}); 

回答

1

出於某種原因,我只是不能將objectStore傳遞給商店(對於dgrid - onDemandGrid)。這次我把我的「數據模型」和我的觀衆分開了。所以,我有這個在app /模型(例如,我的代碼是很模塊化):

define([ 
    "dojo/_base/xhr", 
    "dojo/store/Memory", 
    "dojo/store/Observable"], 
function(
    xhr, 
    Memory, 
    Observable 
){ 

    xhr.get({ 
     url: "app/resources/data/content.json", 
     handleAs: "json", 
     sync: true, 
    }).then(function(data){ 
     contentStore = Observable(Memory({data: data})); 
    }); 

    // global var "song_store" 
    return contentStore; 
}); 

最後,我通過我的商店連接到它(應用/ UI /設計/ ContentGrid)生成我網這樣的。

define([ 
    "dojo/_base/declare", 
    "dijit/_WidgetBase", 
    "dgrid/OnDemandGrid", 
    "dgrid/Keyboard", 
    "dgrid/Selection", 
    "dgrid/extensions/ColumnHider", 
    "dgrid/editor", 
    "app/models/content" 
], function(
    declare, 
    _WidgetBase, 
    Grid, 
    xhr, 
    Memory, 
    ObjectStore, 
    Keyboard, 
    Selection, 
    Hider, 
    editor 
){ 

    return declare([Grid, Keyboard, Selection, Hider], { 
     store: contentStore, 
     /*columns: { 
      selected: editor({ 
        label: " ", 
        autoSave: true, 
        sortable: false 
       }, "checkbox"), 
      Name: "Name", 
      Time: "Duration", 
      Year: "Year", 
      Artist: "Artist", 
      Album: "Album", 
      Genre: "Genre" 
     },*/ 

    columns: { 
     selected: editor({ 
        label: " ", 
        autoSave: true, 
        sortable: false 
       }, "checkbox"), 
     nom: "Name", 
     autodelete: "Auto-delete", 
     groupe_id: "Groupe ID", 
     global: "Global", 
     date: "Date", 
     duree: "Lenght", 
     description: "Description", 
     fichier: "Filename", 
     pleinecran: "Fullscreen", 
     repertoire: "Folder", 
     taille: "Size", 
     expiration: "Expired", 
     id: "id", 
     catergorie: "Category", 
     brouillon: "Example" 
    }, 

    }); 
});