使用ObjectStore(本地json)的Dojo OnDemandGrid(dgrid)

时间:2012-06-29 18:22:28

标签: json dojo dgrid

这段代码有什么问题?我可以获取我的json并使用alert进行调试(因此该部分适用于xhr)...例如,如果我在函数(xhr)中执行此操作,alert(data [0] .name)我得到了正确的值。在网上没有太多的例子...但是指定列并添加对象存储没有显示任何东西......基本上,我只是想加载一些json文件(本地)并在网格上渲染它,但我最终将使用REST在我的应用程序中处理CRUD(所以,我将在不久的将来使用JsonRest。)

我认为它也必须与AJAX一起...我应该将同步设置为true(因为看起来我的全局变量不能正常工作......未定义)。

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 个答案:

答案 0 :(得分:1)

出于某种原因,我无法将objectStore传递给商店(对于dgrid - onDemandGrid)。这次我把我的“数据模型”和我的观众分开了。所以,我在app / models中有这个(例如,我的代码非常模块化):

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;
});

最后,我通过将我的商店附加到它(app / ui / layout / 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"
    },

    });
});