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

Jeff Chimene jchimene at gmail.com
Sun Aug 12 14:57:28 EDT 2007


Eugene Lazutkin wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> 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.
>
> 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.
>
> 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.
>   

Hi Eugene,

Thanks for your patience. I'm not sure how to use checkAcceptance() on 
the source, since I don't know what the target will be (there will be 
several potential targets).

Is there a way to override checkAcceptance() on a DnD target that's 
created using HTML markup?

For example, given the following HTML code:
    <div id="ContainerRoot" dojoType="dojo.dnd.Target" 
class="container"></div>

I can obtain the DOM information using byId(). However, it doesn't look 
like there's anything there to override w/r/t checkAcceptance()

The source is created programatically, so redefining checkAcceptance() 
is not a problem, although it does no good to redefine the DnD source 
instance of checkAcceptance().

I agree that messages are not really the way to solve this problem.
>   
>>     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.
>
>   
>>           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.
>
>   
>>     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. :-)
>
>   
>> 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.
>
> Thanks,
>
> Eugene



More information about the dojo-contributors mailing list