var $ = require('jquery'),
Handlebars = require('handlebars'),
Backbone = require('backbone'),
mainJs = require('./main');
var services = {
authenticationservice: function(collections) {
var jsonreturn;
var api_token = mainJs.get_api_token();
jsonreturn = collections.fetch({
headers: {'Authorization': 'Bearer ' + api_token.access_token},
success: function (collection, response, options) {
var responsejson = JSON.parse(response.responseText);
return responsejson;
},
error: function (collection,response,options){
var errorjson = JSON.parse(response.responseText);
//alert(errorjson.error_description);
return errorjson;
}
});
return jsonreturn;
}
};
module.exports = services;
How do i access the responseText from jsonreturn please help?
Since collections.fetch
is asynchronous in behavior, you should return jQuery deferred as a call to authenticationservice
function. Refer below code
authenticationservice : function(){
var def = $.Deferred();
...
//Somewhere in success
success: function(...){
def.resolve(responsejson);
}
...
//Somewhere in error
error:function(...){
def.reject(errorjson);
}
...
return def;
}
Now the place from where you are calling this service must be aligned to deal with promise like below
service.authenticationservice().then(
function(responsejson){
//Do processing in case of success.
},
function(errorjson){
//Do processing in case of failure
}
);
For more details on JQuery Deferred .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.