Firstly, I’m new to Ionic and AngularJS so bear that in mind as you tear my ugly code apart
Now, a few code snippets of what I currently have before I get into the questions:
A little state snippet:
...
.state('app.beacons', {
url: '/beacons',
views: {
'menu-view': {
templateUrl: 'templates/beacon-list.html',
controller: 'BeaconsCtrl',
resolve: {
beacons: function(BeaconService) {
return BeaconService.list();
}
}
}
}
})
...
A little controller snippet:
.controller('BeaconsCtrl', ['$scope', 'beacons', 'BeaconService', function($scope, beacons, BeaconService) {
$scope.beacons = beacons.results;
$scope.refreshBeaconList = function() {
BeaconService.list().then(function( response ) {
$scope.beacons = response.results
$scope.$broadcast('scroll.refreshComplete');
});
}
$scope.joinBeacon = function( beaconId ) {
BeaconService.updateFireteam( beaconId, 'join' ).then(function() {
$scope.refreshBeaconList();
});
}
$scope.leaveBeacon = function( beaconId ) {
BeaconService.updateFireteam( beaconId, 'leave' ).then(function(){
$scope.refreshBeaconList();
});
}
}])
And finally a little service snippet (I’ve cut the irrelevant parts out of this one):
.service('BeaconService', ['$resource', '$q', 'appConfig', '$ionicLoading', function($resource, $q, appConfig, $ionicLoading ) {
var Beacons = $resource(appConfig.parseRestBaseUrl + 'classes/beacons/:beaconId', {
beaconId : '@beaconId'
}, {
list : {
method : 'GET',
headers: appConfig.parseHttpsHeaders,
params : {
'include' : 'mission,region,platform,level,puser',
'order' : '-createdAt'
}
}
});
return {
list : function() {
var d = $q.defer();
$ionicLoading.show({
template: 'Loading...'
});
var beacons = Beacons.list({}, function( response ) {
$ionicLoading.hide();
d.resolve(response);
}, function() {
$ionicLoading.hide();
});
return d.promise
}
}
}])
So, questions, questions, questions…actually it’s one question but the ramifications for my app as a whole are pretty wide ranging so here it is:
I’m using the “resolve” phase to grab the list of records from the REST API up front and keep my controller code dry. Love that feature.
Problem is, I have a refresh function and two other functions that perform actions that are forcing me to make calls that are resolving a promise in the controller - resolve was supposed to help me stop that. Is there a way around this that I’m not aware of? I think I can just call $state.go on the current state but that feels…wrong somehow…?