Skip to content

Commit

Permalink
refactor(ngBackbone): Update sync layer to use standard then method…
Browse files Browse the repository at this point in the history
… but fallback if necessary
  • Loading branch information
adrianlee44 committed Dec 4, 2016
1 parent 9197b16 commit 0286477
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions ng-backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,56 @@
params.params = options.data;
}

var xhr = ajax(_.extend(params, options)).
success(function(data, status, headers, config) {
options.xhr = {
status: status,
headers: headers,
config: config
};

if (!isUndefined(options.success) && _.isFunction(options.success)) {
options.success(data);
}
}).
error(function(data, status, headers, config) {
options.xhr = {
status: status,
headers: headers,
config: config
};

if (!isUndefined(options.error) && _.isFunction(options.error)) {
options.error(data);
}
});
var successFn = function(response) {
options.xhr = {
status: response.status,
headers: response.headers,
config: response.config
};

if (!isUndefined(options.success) && _.isFunction(options.success)) {
options.success(response.data);
}
};

var errorFn = function(response) {
options.xhr = {
status: response.status,
headers: response.headers,
config: response.config
};

if (!isUndefined(options.error) && _.isFunction(options.error)) {
options.error(response.data);
}
};

var xhr = ajax(_.extend(params, options));

// If $http has a legacy promise
if (!isUndefined(xhr.success) && _.isFunction(xhr.success)) {
xhr
.success(function(data, status, headers, config) {
return successFn({
data: data,
status: status,
headers: headers,
config: config
});
})
.error(function(data, status, headers, config) {
return errorFn({
data: data,
status: status,
headers: headers,
config: config
});
});

// If $http promise comes with `then`
} else {
xhr.then(successFn, errorFn);
}

model.trigger('request', model, xhr, _.extend(params, options));

Expand Down

0 comments on commit 0286477

Please sign in to comment.