在ItemFileWriteStore中添加引用为_reference的新项

Adding a new Item with _reference in ItemFileWriteStore

本文关键字:reference 新项 引用 ItemFileWriteStore 添加      更新时间:2023-09-26

现在我可以使用store.newItem()添加项目了。添加后,它将把它放在旧的JSON数据之后。

但是如何将它放置在children中(查看我的代码)?

{
    'id': 'WMD',
    'name': 'Web MD.',
    'type': 'continent',
    'children': [
        {
            '_reference': 'PUTTANONE'
        }
    ]
}

我该如何将其添加到那个位置?

现在我已经完成了我的问题。我从这个网站得到了这个想法

我仍然使用store.newItem,但必须保留返回的值,并使用store.save

newValueTreeAdded = store.newItem({
    id: name + lastName,
    name: name + " " + lastName,
    type: 'fullName'
});
if(store.isDirty()){
    store.save({onComplete: saveStoreDone, onError: saveStoreFailed});
}

完成后,查看function saveStoreDone

function saveStoreDone(){
    store.fetch({ onComplete: onCompleteFetch });
}

看看function onCompleteFetch

function onCompleteFetch(items, request){
    var selectDepartment = (radioWMDChoose)? 0 : 1; //select root node to add child
    var tempChild = store.getValues(items[selectDepartment],'children');
    var indexChild = tempChild.length;
    tempChild[indexChild] = newValueTreeAdded; //newValueTreeAdded is come from store.newItem
    store.setValues(items[selectDepartment], "children", tempChild);                            
}

完成:D