【问题标题】:Dojo ItemFileWriteStore not reading JSON server fileDojo ItemFileWriteStore 未读取 JSON 服务器文件
【发布时间】:2012-08-02 00:33:04
【问题描述】:

我正在使用 Dojo,它是 dojo/data/ItemFileWriteStore 模块来读取本地服务器上的 JSON 数据文件。在我的 .js 文件中,我有

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 格式。如果没有,请告诉我。 xhrGet 读起来很好。

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

【问题讨论】:

    标签: json dojo


    【解决方案1】:

    ItemFileWriteStore 要求将您的数据结构化为如下所示:

    { 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' }
    ]}
    

    即“标识符”是您的“ID”字段,“标签”是您的“标签”字段,然后是一个名为“项目”的数组中的所有对象。

    您可以在这里查看in ItemFileWriteStore's documentation。如果您的 JSON 数据没有这样的结构,那么您最终可能会使用 IFWS 读取您的文件而实际上没有读取任何数据。

    dojo 1.7 中的其他商店实现不需要这种结构,例如Memory 存储,您可以结合其他文件读取技术来实现相同的目的。

    【讨论】:

    • 感谢 eburgos 的回复。我应该发布我的文件数据。它确实符合该结构。我会将其添加到我的帖子中。让我感到困惑的一件事是,我在这个论坛上看到一些人指出,必须为真正的 JSON 格式引用属性和值。我已经尝试了两种方法,但我的问题是文件没有被获取。我在 Firebug 中查看 Net XHR 活动,但从未触及该文件。此外,当我使用 dojo xhrGet 时,它读取相同的文件就好了。真是奇怪,我也有这个问题。
    • 那么当 Firebug Net XHR 活动没有请求时?
    【解决方案2】:

    尝试使用 dojo.data.ItemFileReadStore 进行读取 json 数据文件,而不是 dojo/data/ItemFileWriteStore。

    注意 dojo.data.ItemFileWriteStore 用于写入 json 数据。

    【讨论】:

    • 我认为 ItemFileWriteStore 也包含 ItemFileReadStore 的功能?如果您是正确的并且它会有所作为,那么具有 IFWS 子集的对象可以工作但 IFWS 本身不能工作,那就太奇怪了。
    【解决方案3】:

    如果您的代码与您在上面发布的代码完全相同,那么解释器可能不喜欢您在 ItemFileWriteStore 分配中省略分号这一事实。尝试添加';'如下:

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

    【讨论】:

      猜你喜欢
      • 2015-02-12
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多