Better jquery ajax calls management

It becomes the same as the original jQuery ajax object.
For example, callbacks can be queued.
get_by_apipk and from_API of ModelObject returns the ajax object.

Example (js):
Account.get_by_apipk('AAA')
    .done(function (data) {
        console.log(data)
    })
    .fail( () => console.log('cool') )
    ...
    .done( ...
This commit is contained in:
Aurélien Delobelle 2017-04-05 04:05:31 +02:00
parent efbcde163b
commit f1aaad7317
2 changed files with 11 additions and 22 deletions

View file

@ -185,24 +185,14 @@ class APIModelObject extends ModelObject {
* Get data of a distant model instance. It sends a GET HTTP request to * Get data of a distant model instance. It sends a GET HTTP request to
* {@link Models.APIModelObject#url_read}. * {@link Models.APIModelObject#url_read}.
* @param {object} [api_options] Additional data appended to the request. * @param {object} [api_options] Additional data appended to the request.
* @param {jQueryAjaxSuccess} [on_success] A function to be called if the request succeeds.
* @param {jQueryAjaxError} [on_error] A function to be called if the request fails.
*/ */
fromAPI(api_options, on_success, on_error) { fromAPI(api_options) {
var that = this;
api_options = api_options || {}; api_options = api_options || {};
on_success = on_success || $.noop;
on_error = on_error || $.noop;
api_options['format'] = 'json'; api_options['format'] = 'json';
$.getJSON(this.url_read, api_options) return $.getJSON(this.url_read, api_options)
.done(function (json, textStatus, jqXHR) { .done( (json) => this.from(json) );
that.from(json);
on_success(json, textStatus, jqXHR);
})
.fail(on_error);
} }
/** /**
@ -213,9 +203,9 @@ class APIModelObject extends ModelObject {
* @param {jQueryAjaxError} [on_error] * @param {jQueryAjaxError} [on_error]
* @see {@link Models.APIModelObject#fromAPI|fromAPI} * @see {@link Models.APIModelObject#fromAPI|fromAPI}
*/ */
get_by_apipk(api_pk, api_options, on_success, on_error) { get_by_apipk(api_pk, api_options) {
this.url_read = this.constructor.url_read_for(api_pk); this.url_read = this.constructor.url_read_for(api_pk);
this.fromAPI(api_options, on_success, on_error); return this.fromAPI(api_options);
} }
/** /**

View file

@ -84,10 +84,9 @@ class AccountManager {
trigramme = trigramme || this.selection.get(); trigramme = trigramme || this.selection.get();
if (trigramme.isValidTri()) { if (trigramme.isValidTri()) {
this.account.get_by_apipk(trigramme, {}, this.account.get_by_apipk(trigramme)
() => this._update_on_success(), .done( () => this._update_on_success() )
() => this.reset_data() .fail( () => this.reset_data() );
);
} else { } else {
this.reset_data(); this.reset_data();
} }
@ -279,9 +278,9 @@ class CheckoutManager {
'last_statement': true, 'last_statement': true,
}; };
this.checkout.get_by_apipk(id, api_options, this.checkout.get_by_apipk(id, api_options)
(data) => this._update_on_success(data), .done( (data) => this._update_on_success(data) )
() => this.reset_data()); .fail( () => this.reset_data() );
if (kpsul.account_manager.is_empty()) { if (kpsul.account_manager.is_empty()) {
kpsul.account_manager.focus(); kpsul.account_manager.focus();