So I asked an earlier question (Original Question). I got a great answer that did exactly what I wanted. However, since I am new to Javascript/Dojo, I wasn’t able to fully understand it, and neither was the answerer of the question.
My question is: How does the following code work?
dndController: function(arg, params){
return new dijit.tree.dndSource(
arg, // don't mess up with the first parameter
dojo.mixin({}, params, {copyOnly:true}))
//create a copy of the params object, but set copyOnly to true
}
So the part bugging me the most is the “args” and “params” parameters. I don’t understand where they come from and what they mean or represent. (If there needs to be more context to the code, I can edit the question later, so just post it in the comments). Also, why couldn’t I just use the new dijit.tree.dndSource directly and why did I need to use the function to return it?
Thanks
Take a look at dijit/Tree.js in the dojo source.
in Tree.js, inside the postCreate function (which is used by any widget as a part of the dijit lifecycle):
You’ll see a section which checks what the
dndControllerproperty is. If it is a string it sets the dndController attribute to the function that creates the class that the string describes (this is whatdojo.getObject(string)is doing).For example if this.dndController was the string
"my.special.dnd.controlller", it would return the function that, when called instantiates a new instance ofmy.special.dnd.controller.It then copies some parameters into an object, followed by executing the function that was either:
(1) looked up via dojo.getObject
(2) uses the custom function you passed in.
I would assume the maintainer of this widget does it this way as some people only need to specify a specific class to use as the dnd controller, whereas others need to do some something more custom based on what parameters the Tree was passed.