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:
parent
efbcde163b
commit
f1aaad7317
2 changed files with 11 additions and 22 deletions
|
@ -185,24 +185,14 @@ class APIModelObject extends ModelObject {
|
|||
* Get data of a distant model instance. It sends a GET HTTP request to
|
||||
* {@link Models.APIModelObject#url_read}.
|
||||
* @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) {
|
||||
var that = this;
|
||||
|
||||
fromAPI(api_options) {
|
||||
api_options = api_options || {};
|
||||
on_success = on_success || $.noop;
|
||||
on_error = on_error || $.noop;
|
||||
|
||||
api_options['format'] = 'json';
|
||||
|
||||
$.getJSON(this.url_read, api_options)
|
||||
.done(function (json, textStatus, jqXHR) {
|
||||
that.from(json);
|
||||
on_success(json, textStatus, jqXHR);
|
||||
})
|
||||
.fail(on_error);
|
||||
return $.getJSON(this.url_read, api_options)
|
||||
.done( (json) => this.from(json) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -213,9 +203,9 @@ class APIModelObject extends ModelObject {
|
|||
* @param {jQueryAjaxError} [on_error]
|
||||
* @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.fromAPI(api_options, on_success, on_error);
|
||||
return this.fromAPI(api_options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -84,10 +84,9 @@ class AccountManager {
|
|||
trigramme = trigramme || this.selection.get();
|
||||
|
||||
if (trigramme.isValidTri()) {
|
||||
this.account.get_by_apipk(trigramme, {},
|
||||
() => this._update_on_success(),
|
||||
() => this.reset_data()
|
||||
);
|
||||
this.account.get_by_apipk(trigramme)
|
||||
.done( () => this._update_on_success() )
|
||||
.fail( () => this.reset_data() );
|
||||
} else {
|
||||
this.reset_data();
|
||||
}
|
||||
|
@ -279,9 +278,9 @@ class CheckoutManager {
|
|||
'last_statement': true,
|
||||
};
|
||||
|
||||
this.checkout.get_by_apipk(id, api_options,
|
||||
(data) => this._update_on_success(data),
|
||||
() => this.reset_data());
|
||||
this.checkout.get_by_apipk(id, api_options)
|
||||
.done( (data) => this._update_on_success(data) )
|
||||
.fail( () => this.reset_data() );
|
||||
|
||||
if (kpsul.account_manager.is_empty()) {
|
||||
kpsul.account_manager.focus();
|
||||
|
|
Loading…
Add table
Reference in a new issue