I am currently attempting to use a resolve
with a promise
in response to an issue with filters that I am currently tackling. However, my resolve function is not yet functioning as expected.
I have decided to implement this approach based on advice I received from others in a previous query where someone recommended using a resolve as the most logical solution.
This is the error message displayed in the console:
localhost:1337/lines/sports/undefined:1
GET http://localhost:1337/lines/sports/undefined 400 (Bad Request)
Please review my code below:
app.js
.state('app.sports', {
url:'/sports',
views:{
menuContent:{
templateUrl:'templates/sportsList.html',
controller:'SportsController',
resolve: {
Sports: function(SportsFactory, AuthFactory, $q) {
var defer = $q.defer();
console.log(AuthFactory);
AuthFactory.getCustomer().then(function() {
SportsFactory.getSports().then(function(sports) {
defer.resolve(sports);
});
});
return defer.promise;
}
}
controller.js
.controller('SportsController', function($scope, $state,
AuthFactory, SportsFactory, Sports) {
$scope.query = '';
$scope.sports = Sports;
$scope.sports = [];
$scope.customer = {};
});
AuthFactory.getCustomer().then(function(customer) {
$scope.customer = customer;
SportsFactory.getSportsWithLeagues(customer).then(function(sports) {
if (sports.length) {
$scope.sports = sports;
}else {
AuthFactory.logout();
}
}, function(err) {
console.log(err);
});
}, function(err) {
console.log(err);
});
service.js
.factory('SportsFactory', function($http, $q, AuthFactory,
LocalForageFactory, LeaguesFactory, CONSTANT_VARS) {
return {
getSports: function(agent) {
var defer = $q.defer();
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS)
.then(function(sports) {
if (!_.isNull(sports)) {
defer.resolve(_.values(sports));
}else {
$http.get(CONSTANT_VARS.BACKEND_URL + '/lines/sports/' + agent)
.success(function(sports) {
//forcing array instead of object
sports = _.values(sports);
sports = _.sortBy(sports, function(sport) {
return sport.priority;
});
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS, sports);
defer.resolve(sports);
})
.error(function(err) {
defer.reject(err);
});
}
});
return defer.promise;
},
getSportsWithLeagues: function(customer) {
var _this = this,
defer = $q.defer(),
rejection = function(err) {
defer.reject(err);
},
sportsLength;
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES)
.then(function(sportLeagues) {
if (!_.isNull(sportLeagues)) {
//forcing array instead of object
sportLeagues = _.values(sportLeagues);
defer.resolve(sportLeagues);
}else {
_this.getSports(customer.agent).then(function(sports) {
sportsLength = sports.length;
LeaguesFactory.getLeagues({
sportIds: _.pluck(sports, 'id'),
lineProfile: customer.lineProfile,
betLimit: customer.betLimit
}).then(function(leagues) {
_.each(sports, function(sport) {
sport.leagues = _.filter(leagues, function(league) {
return sport.id === league.sport.id;
});
});
//forcing array instead of object
sports = _.values(sports);
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES, sports);
defer.resolve(sports);
}, rejection);
}, rejection);
}
}, rejection);
return defer.promise;
}
};
});
and this is the authFactory:
.factory('AuthFactory', function($q, $http, $state,
LocalForageFactory, CONSTANT_VARS) {
return {
/**
* This function logs the customer, if the customer exists,
* Customer data is saved in order to perform actions,
* if not, an error message is returned.
* @param credentials a json with this format {username: 'jhon', password:'D03'}
* @returns {Animation.promise|promise}
*/
login: function(credentials) {
var defer = $q.defer(),
_this = this;
$http.post(CONSTANT_VARS.BACKEND_URL + '/auth/login',
credentials
).success(function(data) {
if (data.error) {
defer.reject(data);
}
_this.setCustomer(data).then(function(customer) {
defer.resolve(customer);
}, function(err) {
defer.reject(err);
});
}).error(function(data, status) {
if (status === 0) {
data = new Error('Backend is down');
data.raw = {};
}
defer.reject(data);
});
return defer.promise;
},
setCustomer: function(customer) {
var defer = $q.defer();
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_CUSTOMER, customer).then(function(customer) {
/*Removing LocalForage Items*/
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_LEAGUES);
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES);
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS);
defer.resolve(customer);
}, function(err) {
$state.go('app.login');
defer.reject(err);
});
return defer.promise;
},
updateCustomer: function(customer) {
var defer = $q.defer();
LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_CUSTOMER, customer).then(function(customer) {
defer.resolve(customer);
}, function(err) {
$state.go('app.login');
defer.reject(err);
});
return defer.promise;
},
getCustomer: function() {
var defer = $q.defer();
LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_CUSTOMER).then(function(customer) {
if (customer) {
defer.resolve(customer);
}else {
defer.reject(new Error());
}
defer.reject(customer);
}, function(err) {
defer.reject(err);
});
return defer.promise;
},
logout: function() {
var defer = $q.defer();
this.getCustomer().then(function(credentials) {
$http.post(CONSTANT_VARS.BACKEND_URL + '/auth/logout',
credentials
).success(function(data) {
if (data.error) {
defer.reject(data);
}
/*Removing LocalForage Items*/
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_LEAGUES);
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES);
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS);
LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_CUSTOMER);
defer.resolve(data);
}).error(function(data) {
defer.reject(data);
});
}, function(err) {
$state.go('app.login');
defer.reject(err);
});
return defer.promise;
},
updateToken: function(token) {
var _this = this,
defer = $q.defer();
this.getCustomer().then(function(customer) {
customer.token = token;
_this.updateCustomer(customer).then(function(savedCustomer) {
defer.resolve(savedCustomer);
}, function(err) {
defer.reject(err);
});
}, function(err) {
defer.reject(err);
});
return defer.promise;
},
customerInfo: function() {
var defer = $q.defer();
this.getCustomer().then(function(customer) {
$http.post(CONSTANT_VARS.BACKEND_URL + '/auth/info', customer)
.success(function(data) {
defer.resolve(data);
})
.error(function(err) {
defer.reject(err);
});
}, function(err) {
defer.reject(err);
});
return defer.promise;
}
};
});