Show last content with back button


#1

I’ve got list with search filters and remote data. After clicking item details occur.
When I press back button I want to show the same list (the same filters applied) without loading it from Internet.
Is it possible?

AppCtrl.controller('atrakcjeCtrl', function($scope, App, $http) {
  var params = App.getParams();
  if (params) {
	$scope.filtry = {};
	if (params.bliskoCiebie) {
	  var geoLokalizacja = App.getGeoLokalizacja();
	  $scope.filtry.x = geoLokalizacja.x;
	  $scope.filtry.y = geoLokalizacja.y;
	} else {
	  if (params.x)
		$scope.filtry.x = params.x;
	  if (params.y)
		$scope.filtry.y = params.y;
	}
	$scope.filtry.rodzaj = '';
	$scope.filtry.sortuj = 'odleglosci';
	$scope.filtry.w_promieniu = 50;

	$scope.pobierzListeAtrakcji = function() {
	  $http({
		method: "POST",
		url: App.getPsURL() + "atrakcje.php",
		data: this.filtry
	  }).success(function(data) {
		$scope.atrakcje = data;
	  });
	};
	$scope.pobierzListeAtrakcji();
	App.clearParams();
  }
});

#2

You need to use a service for this. You $http method in the controller will never allow you to “cache” the results. It will be fired every time.

So, create a service. When the controller first queries the service, the service will store the results in a variable. The next time the controller is loaded, it needs to ask the service for the data again. Then, if the service has any data stored, it will provide it, otherwise, it will query the API again.

Here is an example of using a service : http://codepen.io/calendee/pen/FisDH


#3

Hi @Calendee, was reviewing your service example above and it looks to me that MovieService.get is what actually emulates a real api query and not MovieService.getSync which merely returns the js object. How would you actually go about caching your service results in a real app? Would you opt for something like angular-cache or merely store results in localStorage the first time you hit your service and load from localStorage subsequently so that the app can work just fine offline?
Take something like Twitter app as an example.


#4

It depends on the purpose of the “cache”. If you don’t need it for offline use, I’d just put it in a variable. Then, it will exist for the life of the app. If the app is completely closed, it will be destroyed. Otherwise, it will be available as long as the app stays open.

If you need real persistence or expirations or more features, then localStorage or Angular-cache(which is what I use.


#5

So tip for next feature - load view from last cache when pressing back button.
Now I have to recreate last state of view :confused:


#6

I looked at Angular-cache (http://angular-data.codetrain.io/) and I have to say a bit of work with that.
I think that option would be very useful.
Can we have hope that You include posiibility “load view from cache when pressing back button” in near future?


#7

There is a feature request in for this already.

Issue # 721 : https://github.com/driftyco/ionic/issues/721 and
Issue # 714 : https://github.com/driftyco/ionic/issues/714


#8

Great news! Looking forward