Last modified: 2012-06-23 14:03:55 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T38783, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 36783 - Implement Promise interface in mediawiki.api module
Implement Promise interface in mediawiki.api module
Status: RESOLVED FIXED
Product: MediaWiki
Classification: Unclassified
JavaScript (Other open bugs)
unspecified
All All
: Normal enhancement (vote)
: 1.20.0 release
Assigned To: Krinkle
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-05-11 20:42 UTC by Daniel A. R. Werner
Modified: 2012-06-23 14:03 UTC (History)
2 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Daniel A. R. Werner 2012-05-11 20:42:36 UTC
mw.api.prototype.ajax and mw.api.prototype.post in JavaScript are offering 'err' and 'ok' in the ajaxOptions parameter to specify callbacks for error and success.
It might be worth replacing this with the use of jQuery.Deferred, so instead of returning the jqXHR objectd they could return a new Deferred object so one can assign several callbacks to them.
The jqXHR object shouldn't be of interest much I assume since it only tells about the ajax requests state and whether it failed, not about the api calls success. So the ajax can succeed but the api request could still have failed, this is not apparent when using the jqXHR object.

If people use the jqXHR for some reason code could break if we would change the return values.
Right now the 'ok' option is obligatory, we could change that so it isn't anymore and if it isn't provided, we would just return a custom deferred object.
Comment 1 Krinkle 2012-05-15 08:50:07 UTC
I'm not sure what you are asking here.

The only mention of jqXHR is in the comments of mw.Api. The method itself is a passthru from jQuery.ajax (the last line in both of the implementations is "return $.ajax( opt );"

As of jQuery 1.5 jqXHR extends jQuery.Deferred() so it implements the promise interface etc.

The following should work fine:

<code>
function myApiStuff() {
  var api = new mw.Api();
  return api.get( {}, ... );
}

var apiStuff = myApiStuff();
apiStuff.always( fn );
apiStuff.done( fn );
// etc.

</code>

See also http://api.jquery.com/jQuery.ajax/

A few things we could do in addition to that:
* Make the "ok" argument to mw.Api..get optional, so that one can use the deferred methods without having to pass a bogus function as well.
Comment 2 Krinkle 2012-05-15 08:53:51 UTC
Ah, I get what you mean now. Those methods tie into the ajax response directly, whereas the ok/err callbacks from mw.Api do some Api specific post-processing first.

So yeah, we should indeed create our own Deferred object that waits for jqXHR's promise first.
Comment 3 Krinkle 2012-06-07 18:10:24 UTC
Working on it.
Comment 4 Krinkle 2012-06-07 20:47:19 UTC
Change-Id: I2986eec3c8827dcca34a17faf7ebeb80fd770b08
Comment 5 Antoine "hashar" Musso (WMF) 2012-06-10 09:29:56 UTC
https://gerrit.wikimedia.org/r/10662

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links