I need to override onClick method in the Tree.js. Is there any common way to override dojo/dijit classes methods?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
You could use:
I'm a bit confused, since you were already doing this in the last question you posted.
You've got a few choices, depending on what you want to do.
Clobbering method stubs
In the case of true stubs like
onClick
, it's potentially as easy as clobbering that method on your widget instance.Programmatically:
Or declaratively (this is exactly the same as what you were doing in your last question):
Connecting to method invocations
In other cases, perhaps you need to do something whenever a given method gets called, in which case you could use the widget's
connect
method (which is a nicer version ofdojo.connect
that will automatically clear itself when the widget is destroyed). In that case you could do something like this:Programmatically:
Declaratively, this can be done very similarly to the above, except instead of
type="dojo/method"
, make sure you usetype="dojo/connect"
Note that when you connect like this, your code will execute after the method whose invocation you are connecting to. If you need to do something before, your best option is probably to
dojo.declare
your own extension to the widget. If you need to go that far, I'll elaborate, but I think you'll likely be set with one of the above options.Edit #1: Connecting the dots (no pun intended...oh heck, yes it was)
Since it seems that my comment appended to my answer to the original question was somehow not clear enough, here's a code block modifying the original code in that question based on the simple two steps exactly as I explained in that comment. The only tiny wrinkle is that the arguments passed to
_onClick
are slightly different.This solution may feel a bit sub-optimal since it involves connecting to a method that's suggested to be private. However, it's a way that should work regardless of whether
openOnClick
is true or not. If you are certain you're going to haveopenOnClick
set totrue
, you could potentially write a single function, then connect it to bothonClick
andonOpen
instead (both get passed the item, then the node).Edit #2: Common functions, connecting programmatically
To answer your follow-up questions, I'd like to actually address them in reverse order - since if you are interested in connecting programmatically, it will actually make the other question easier to answer.
So first, to answer your
connect
question: you definitely don't want to be usingdojo.byId
, as that's not giving you the Tree widget, that's giving you some DOM node (probably the topmost) associated with the widget. As a general rule,dojo
methods don't know anything aboutdijit
stuff.What you do want to be doing, is what I suggested above. Here it is applied as per the code you attempted. Also note that
onClick
has a capital C - another general rule: widget events use camel case notation, as a way to distinguish them from plain DOM events which don't.Now, to take that a step further and resolve your other inquiry, if you want to bind some common functionality to
onClick
andonOpen
andonClose
, you could define a function first, then connect it to both. This is one of the many things that makes JavaScript awesome - the availability of functions as first-class objects that can be easily passed around.Now there's one important remaining question: where should we do this? The best place is likely inside a function passed to
dojo.ready
(ordojo.addOnLoad
, same thing,ready
was added as a synonym in 1.4) so that it will run only after:dojo.require
d modules have loadedparseOnLoad: true
indjConfig
, all widgets defined in the document's HTML will already be instantiatedSo, sometime after your
dojo.require
s, in script, you'd do this:Give it a shot.
Also, if you're interested in a bit of reading, I've actually written about a couple of topics I touched on in this edit:
dojo.require
d Reading