Opened 7 years ago

Last modified 18 months ago

#15660 reopened feature

dojo/store/Memory: make put() support "parent" and "before" options

Reported by: bill Owned by: Kris Zyp
Priority: high Milestone: 1.14
Component: Data Version: 1.7.3
Keywords: Cc:
Blocked By: Blocking: #15661

Description (last modified by bill)

As the doc explains, stores can support inserting an item as a specific child of another item (as opposed to the last child. dojo/store/Memory doesn't support this. But it's needed for dijit/Tree. (Currently the DnD example of Tree, dijit/tests/tree/test_tree_Dnd.html, is running off of ItemFileWriteStore due to this limitation.)

A colleague (Anthony Erwin, IBM, CCLA) created a subclass of Memory to support it, perhaps you can use that code as a base. Obviously you would need tests too.

         // Need to override put to support DnD and node ordering
        put: function (object, options){
             if (options) {
                         // Per tree example online:
                         //             To support DnD, the store must support put(child, {parent: parent}).
                         //     Since memory store doesn't, we hack it.
                         //     Since our store is relational, that just amounts to setting child.parent
                         //     to the parent's id. 
                         if  (options.parent){
                                object.parent = options.parent.id;
                                
                                 //Another hack to cause items moved into a new parent to show up as the 
                                 //last child of the parent
                                 if  (!options.before) {
                                         this .remove(object.id);
                                }
                        }
                        
                         // What the examples don't tell you is that to support betweenThreshold on the tree
                         // (and support setting the order of nodes), we also need to deal with the "before"
                         // attribute (passed in by model's newItem and pasteItem implementations). So, hacking that
                         // in as well.
                         if  (options.before) {
                                data = this .data;
                                index = this .index,
                                idProperty = this .idProperty;
                                 var  id = object[idProperty] = (options && "id"   in  options) ? options.id : idProperty in  object ? object[idProperty] : Math.random();
                                 if (id in  index){                                     
                                         //Remove the object from its current position
                                         this .remove(id);
                                }
                                 //Insert/add it in right position
                                index = this .index; //there's a re-index after the remove operation
                                 var  beforeRefId = options.before.id;
                                 if (beforeRefId in  index){
                                         //carve out a spot for the new item
                                        data.splice(index[beforeRefId], 0, object);
                                         // now we have to reindex
                                         this .setData(data);
                                }
                        }
            }
             return   this .inherited(arguments);
        }

Attachments (3)

Memory.patch (2.7 KB) - added by sunxcint 5 years ago.
Path for dojo/store/Memory
test_memory.patch (2.7 KB) - added by sunxcint 5 years ago.
Memory.2.patch (2.7 KB) - added by sunxcint 5 years ago.
Patch for Memory store.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 7 years ago by bill

Blocking: 15661 added

comment:2 Changed 7 years ago by bill

Description: modified (diff)
Milestone: tbd2.0

Marking this for 2.0 since we will need it by then for Tree, since dojo/data is presumably going away.

comment:3 Changed 5 years ago by sunxcint

I added a patch. I do not update dijit/tests/tree/test_tree_Dnd.html. But I tested a memory-based Dnd tree with the new memory code locally and it works well.

Changed 5 years ago by sunxcint

Attachment: Memory.patch added

Path for dojo/store/Memory

Changed 5 years ago by sunxcint

Attachment: test_memory.patch added

Changed 5 years ago by sunxcint

Attachment: Memory.2.patch added

Patch for Memory store.

comment:4 Changed 5 years ago by sunxcint

Memory.patch and Memory.2.patch are virtually from the same file.

comment:5 Changed 5 years ago by Denis Rykov

Is this patch reflects only one approach when children knows about their parent? In my case some of objects has "children" property, but children doesn't know about parents.

comment:6 Changed 3 years ago by dylan

Milestone: 2.01.11
Resolution: fixed
Status: newclosed

This is handed in dstore ( http://dstorejs.io/ ), the successor of dojo/store.

comment:7 Changed 3 years ago by dylan

Milestone: 1.111.12
Priority: undecidedhigh
Resolution: fixed
Status: closedreopened

comment:8 Changed 2 years ago by dylan

Milestone: 1.121.13

Ticket planning... move current 1.12 tickets out to 1.13 that likely won't get fixed in 1.12.

comment:9 Changed 18 months ago by dylan

Milestone: 1.131.14
Note: See TracTickets for help on using tickets.