2

I'm new to Ionic Framework and AngularJs as well.

The app I'm working on has a view where user can search and the results are presented in a list. When the user clicks one of the list items, it navigates to details view. When user presses the "back" button at the header bar, the search text box and results list are empty and he must search again.

What is the recommended approach to, when the user presses the back button from details view, bring the search view populated with the previous search term and results? (would be nice to have the scroll position restored as well)

I know I can just store this information into some service, but this sounds like a lot of work.

Are there any other cool stuff like a view state service or something that can do this for me?

Auresco82
  • 603
  • 6
  • 14

1 Answers1

1

I know I can just store this information into some service, but this sounds like a lot of work.

really?

app.factory('searchData', function() {
  return {
    searchTerm: '',
    results: []
  };
});

that's how it's done. But it sounds like what you're really looking for is a cure for laziness...

Gil Birman
  • 35,242
  • 14
  • 75
  • 119
  • Ok, ok, why are you so angry? :) I did like you suggested, but since it is that simple, the framework could do this for us. – Auresco82 May 15 '14 at 15:48
  • After you write the code it does it for you :) If you want something even simpler use `app.constant('searchData', { searchTerm: '', results:[] })` instead – Gil Birman May 15 '14 at 21:47
  • I am facing the same problem. In other mobile frameworks I have used you could cache the state of the list view when going to the detail and when you returned it was restored for you. So it sounds like in Ionic you have to save and restore the state manually - is this definitely the case? IF so, Gil's approach sounds good, however I was hoping for a framework approach to this. FWIW - this is discussed here: http://forum.ionicframework.com/t/save-list-view-state/1554 Moving to the Detail page destroy's the List Controller's scope, which make sense. I also cache my $resource calls. – Rodney Sep 26 '14 at 00:13
  • I implemented a solution using $rootScope to save and restore state between views and ALSO in a Service (with a Getter and Setter) - here's a more detailed explanation between the two: http://stackoverflow.com/questions/11938380/global-variables-in-angularjs - someone found this in the Agnular FAQ about $rootScope: "If you're tempted to put a function on $rootScope, it's almost always better to put it in a service that can be injected where it's needed, and more easily tested. Conversely, don't create a service whose only purpose in life is to store and return bits of data.". So it depends ;) – Rodney Sep 26 '14 at 22:12
  • Last comment: As per recommendations, I am storing only the SearchTerm and not the data itself, which is cached. So now I am not sure whether to use the $rootScope or Service way, as the Angular FAQ is clear on this. However, I now know how to do both options, which is good – Rodney Sep 26 '14 at 22:19