简体   繁体   English

尝试将新项目添加到JSON存储时,出现“ newItem()未传递新项目的标识”错误

[英]I'm getting a “newItem() was not passed an identity for the new item” error while trying to add a new item to a JSON store

I've seen other posts in this site regarding the same issue and I've tried the solutions given. 我在该网站上也看到过其他有关同一问题的文章,并且已经尝试了给出的解决方案。 I've also visited the links that may offer a solution but I'm still stuck with the same error. 我也访问了可能提供解决方案的链接,但仍然遇到相同的错误。

I'm using DOJO and something as simple as this won't even work 我正在使用DOJO,而简单的操作甚至无法正常工作

    myStore.newItem({id: 'test', otherfield: 'otherinfohere'});
    myStore.save();

Supposedly the "newItem() was not passed an identity for the new item" error appears when you haven't provided an identifier for the new item, which i have. 当您没有为新项目提供标识符时,就会出现“ newItem()未传递新项目的标识”错误。 The whole purpose of this (Just in case anyone can provide a good idea or has done something similar before) is that i want to create a data grid that shows info from a particular store. 这样做的整个目的(以防万一有人可以提供一个好主意或做过类似的事情)是我想创建一个数据网格,以显示来自特定商店的信息。 The problem is, that in that store all the items may not have the same structure. 问题在于,在该商店中,所有商品可能没有相同的结构。 For instance: 例如:

I may have a store that looks like this 我可能有一家看起来像这样的商店

{identifier: 'id',
              label: 'name',
              items: [
                { id:'1', name:'Ecuador',           capital:'Quito' },
                { id:'2', name:'Egypt',             capital:'Cairo' },
                { id:'3', name:'El Salvador',       capital:'San Salvador' , additionalField: 'otherinfohere'},
                { abbr:'gq', name:'Equatorial Guinea', capital:'Malabo', additionalField: 'otherinfohere'},
        ]}

This is possible because I'm the one constructing the store in a Spring Controller (I'm also using the Spring Framework) from information I have locally stored in a Berkeley DB. 这是可能的,因为我是根据本地存储在Berkeley DB中的信息在Spring Controller中构造商店(我也在使用Spring Framework)的人。 So what i need is a data grid with a dynamic layout because I don't want blank spaces to show in the view in the rows with lesser amount of fields, and i need to show all the info in the store at the same time, but i don't know how to do this. 因此,我需要的是一种具有动态布局的数据网格,因为我不希望在字段数量较少的行的视图中显示空格,并且我需要同时显示存储中的所有信息,但我不知道该怎么做。 I thought of doing it by creating a simple layout of only 1 field. 我想通过创建仅1个字段的简单布局来做到这一点。 In it I would load data from a store i create dynamically at runtime. 在其中,我将从运行时动态创建的存储中加载数据。 The data in the store would be composed of HTML combined with the values coming from the original store so I could obtain something like this, which is inside an attribute of a JavaScript Object and let the browser parse it for me: 存储中的数据将由HTML结合原始存储中的值组成,因此我可以获得类似的内容,它位于JavaScript Object的属性内,然后让浏览器为我解析:

    <div><span>id: originalID </span>....</div>

This of course is a simple example, the html layout i'm looking for is far more complicated, but i think that passing it as a string to an object might do the trick. 当然,这是一个简单的示例,我要查找的html布局要复杂得多,但是我认为将其作为字符串传递给对象可能会达到目的。 The problem is that i don't even know if that idea will work because i get that error whenever i try to add values to my secondary store. 问题是我什至不知道该想法是否会奏效,因为每当我尝试向辅助存储中添加值时,都会收到该错误。

rdb.modules.monitor.historicStore.fetch({onComplete: function(items, request){
            for (var i = 0; i < items.length; i++){
                var item = items[i];
                var obj = new Object();            
                obj.id = rdb.modules.monitor.historicStore.getValue(item, "id");;
                var html = "<div><span>";
                html += rdb.modules.monitor.historicStore.getValue(item, "sql");
                html += "</span></div>";
                obj.html = html;
                myStore.store.newItem(obj);                 
            }
        }});

In this context "historicStore" refers to the JSON store that has the values that i need to convert and add to "myStore" after i added some HTML. 在这种情况下,“ historicStore”是指JSON存储,该存储具有在添加一些HTML之后需要转换并添加到“ myStore”的值。

I hope you got the main idea of what I'm trying to do. 我希望您对我正在尝试做的主要想法有所了解。 If anyone can help me we either of these problems i would really appreciate it. 如果有人可以帮助我,我们将不胜感激。 Thanks in advance 提前致谢

For the issue regarding store:- "id" is mandatory for a store, if it is going to be used for a grid(datagrid, EnhancedGrid, etc. whatever). 对于与商店有关的问题:-如果要用于网格(数据网格,EnhancedGrid等),则“ id”对于商店是必需的。 The items are handled only on basis of "id" attribute by the grid data structures. 仅通过网格数据结构基于“ id”属性来处理项目。

Usually, id can be a loop variable/ auto incrementation, to avoid any cases like you have said. 通常,id可以是循环变量/自动递增,以避免出现您所说的任何情况。 Before adding the store to the grid, ensure that all items have the id attribute. 在将商店添加到网格之前,请确保所有商品都具有id属性。 You can write a function which will loop through each item and check for this, else add an auto-incrementing value for the id attribute of that item. 您可以编写一个函数,该函数将遍历每个项目并进行检查,否则为该项目的id属性添加一个自动递增的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM