Yes, it will ensure the code won't run if that particular toolbar isn't enabled. And it is also a safe guard for backwards compatibility (since the API used to have a different interface), and forwards compatibility (it might change).
Usually you wouldn't need such guard and instead use a dependency, but because you don't want to trigger a load of this module (you merely want to hook into it if the toolbar is there, you don't want to load another toolbar which is what adding a dependency would do).
— Krinkle
On 23 Apr 2014, at 14:21, Justin Folvarcik jfolvarcik@gmail.com wrote:
Am I correct in assuming that adding a check for mw.toolbar will help prevent the code from causing errors when in an edit view without a toolbar?
On Wed, Apr 23, 2014 at 7:14 AM, Krinkle krinklemail@gmail.com wrote:
On 22 Apr 2014, at 02:37, Lego KTM legoktm.wikipedia@gmail.com wrote:
On Mon, Apr 21, 2014 at 4:09 PM, Justin Folvarcik jfolvarcik@gmail.com
wrote:
function removeDuplicateLinks(){
.. } if (wgAction == 'edit'){ mw.toolbar.addButton( { imageFile: ' http://localhost/wikidev/images/2/20/Button_cite_template.png', speedTip: 'Remove duplicate links', callback: removeDuplicateLinks(),
Change this line to "callback: removeDuplicateLinks".
Your code had "removeDuplicateLinks()", which would execute the function, and set the return value as the callback, while you wanted the actual function. Simply removing the () fixes that.
Indeed. That was the reason it ran on page load, because it was being invoked directly by your code when creating the button object.
To Justin: Please also change the code to extend the if-statement from `( wgAction === "edit" )` to be `( wgAction === "edit" && mw.toolbar )`.
On 22 Apr 2014, at 15:42, Erwin Dokter erwin@darcoury.nl wrote:
What I *suspect* you are doing wrong is... using the 'callback:'
parameter. I think this is the addButton's function callback. It stands to reason that it would be executed once the addButton function has done its work.
From what I have been able to discern from the badly organized
documentation (again, I may be totally misguided), is that you want to use the 'action:' parameter instead.
The mw.toolbar interface neither documents nor implements any such function. There is no such thing as button.callback or button.action, and for as long as I can remember, such feature never existed.
As for documentation, this particular method is quite well-documented:
https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.toolbar-method-...
If you need a callback, I'd recommend you re-enable the WikiEditor toolbar instead of the legacy classic toolbar, and use its API to add a button instead.
The WikiEditor API provides a wide range of features, including a callback.
— Krinkle
Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l
--
Justin Folvarcik *"When the power of love overcomes the love of power, the world will finally know peace."*-Jimi Hendrix _______________________________________________ Wikitech-l mailing list Wikitech-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/wikitech-l