[dojo-contributors] HOWTO: retrieve a 0.9 DnD object

Jeff Chimene jchimene at gmail.com
Tue Jul 31 23:28:27 EDT 2007


Eugene Lazutkin wrote:
> Jeff Chimene wrote:
> > Hi,
>
> > Is it possible to retrieve a value associated with a DnD node?
>
> Yes. If you have a node and its source:
>
> - source.map[node.id].data --- gives you the access to the associated
> data object, whatever it is.
>
> - source.map[node.id].type --- gives you an array of associated types
> (strings) for this node.

Am I correct that these values aren't available once the drag operation
starts?

>
> The easiest way to do it is to specify a creator function associated
> with this source. The creator takes two parameters: a data item ,and an
> optional hint (a string). A hint can be "avatar" (for the avatar
> rendering) or undefined. It should return an object with 3 values:-
> node --- a node, which is supposed to reflect some aspect of your
> data, a visual representation, if you will.
>
> - data --- a data item, which can be retrieved later as I described above.
>
> - type --- an array of associated types described above.
>
> Example: you can have a full blown HR record (data) for every person but
> represent them visually (node) only using their names. And you can
> access the original object regardless of how it is represented.

Yes, I can see this. I didn't ask the question correctly: can I access
the information once it's in flight, or dropped in a target?

>
> If you generate DnD items inline using the markup, the default creator
> will assume that the node's content (innerHTML) is a data, and the type
> is ["text"].
>
> > SCENARIO:
>
> >     I'm trying to write an example for the 0.9 Dojo book that shows how
> >     to use DnD.
>
> >     This example works with a JSON store that has three items: name,
> >     weight, value.
>
> >     I can create successfully create DnD source, target and listen on
> >     the "/dnd/drop" message.
>
> >     I'd like to abort the move when an accumulator associated with the
> >     target reaches a preset limit. To do this, I want to retrieve the
>
> The simple way to control your source is to re-define checkAcceptance()
> method on the source. Basically it checks if source/target can accept a
> given set of items. You can return "false" as soon as you hit your limit.

I'll try this.
>
> >     weight associated with the DnD node when I hear the /dnd/drop
> >     message. Using dojo/tests/dnd/test_dnd.html as a baseline, I can see
> >     how to embed a function in a DnD node. Currently, I have two
> >     functions: (1) toString; (2) getWeight. toString() has already done
> >     its job creating the DnD node text. I cannot see how to call
> >     getWeight() when I hear the /dnd/drop message.
>
> > PROBLEMS:
>
> >     I see that two of the explicit arguments to the /dnd/drop callback
> >     are the DOM node, and the source map. Unfortunately, neither of
> >     these items are useful:
>
> >           o The DOM node only has the node text. It doesn't have the
> >             weight or value associated with the DnD node as it was
> >             created and;
>
> Probably you should use a non-default creator --- that's why it is here
> in the first place.

Right. I'm using the non-default creator. Am I correct in stating that I
cannot get at the getWeight() method once the node has left the source
container?
>
> >           o The source map has already been updated to remove the
> >             dragged item.
>
> Yes, this is the case --- it is assumed that the data has meaning only
> for the target and the source. It was not meant for 3rd parties to
> expect the data transfer. 3rd-party listeners can use this topic as a
> notification. If you have a good cause to change it, let me know.

Well, doesn't that pretty much eliminate using source.map[] during or at
the end of the drag operation to get at information created by the
custom creator function? I can see source.map[] using the debugger. The
problem is that I can't see how to get at the target node's source.map
during or after the node transfer operation.

>
> >     If I could re-read the ItemFileReadStore, that would be fine.
> >     Unfortunately, I cannot see how to query ItemFileReadStore given a
> >     key value. Query seems to use Query By Example; which technique uses
> >     string constants, not variables. I have not figured out how to stash
> >     an ItemFileReadStore item in a DnD node.
>
> >     The implicit argument to the /dnd/drop callback is the context. This
> >     doesn't really help, as the context would either be the
> >     ItemFileReadStore item, or the DnD node. This means registering the
> >     listener function /N/ times, where /N/ is the ItemFileReadStore item
> >     cardinality. Registering a callback that many times doesn't make
> sense.
>
> >     Registering a /dnd/start callback also fails with the same issues.
> >     The argument list contains the DOM node, not the DnD node.
>
> You lost me here. :-)

Sorry. I probably shouldn't have asked this the way I did. It was too
confusing.

>
> > I'd simply override the functions if I were writing this application
> > privately. I don't really see that as an option because of the public
> > nature of this application.
>
> You can still override methods. For example, test_dnd.html does it
> inline using the Dojo markup to specify a custom creator

I'm already using the custom creator technique, so I guess this isn't an
issue... I suppose the custom creator function was so intuitive that it
didn't feel like I was overriding an important method to accomplish some
obscure purpose or overcome a library design flaw.

Unless I'm wrong, source.map[] is only for nodes before the DnD
operation starts? That negates its value in solving the problem at hand.
Of course, I'm not thinking outside the box; you answered the "howto" by
overriding checkAcceptance(). I'm looking forward to trying that
technique...

Thanks for your help!

BTW, this is for example three in "Life w/ Dojo"

Cheers,
jec


More information about the dojo-contributors mailing list