View cache, good practices

I am doing the Pluralsight course of Ionic (changes here and there, but so far so good) and I ran into an issue and well, it was the cache. I dug a little bit on it and I am asking for good practices. Let me simplify the idea (different topic than the course):

List of car brands (at /brands), when I click on one brand, in my api service I set the brand and I navigate to /brand.

In /brand it calls the service, service knows the selected brand and request all its cars. So I can see a list of cars of that brand. Then I click one car and I go to /car/:id and I see some details.

I go back to the home page (/brands) and I click a different brand, I notify the api service to change its brand and I navigate to /brand and it shows the old brand cars. In other words, what you see in /brand depend on a variable of the api service (AKA the first queried brand)

So what I saw here is that states are cached per parameter, so /brand is always cached and also I have a cached /car/1 and a cached /car/2.

So the issue here is since /brand is cached, every time I hit that state, no matter if I changed the brand internally, it is going to load the wrong data.

I can simply disable cache for that view, but in the case of this app, there are at least 4-5 states like that so I need to find a better way.

Maybe the whole idea of having the brand saved on the api is a bad idea and I should have stuff like /brand/1 and /brand/2 so I never hit a state with the wrong data.

If that is the good idea, I have another question. Is an abstract state (a side menu) also cached? I can use that as well to load my states using params, but if that is going to be cached, I can’t change the params there. Maybe I can disable the cache for side menu.

Thoughts? :slight_smile:

I don’t know about best practices, but another solution would be to listen to the $ionicView.enter Event on the $scope and update the DOM/$scope accordingly.

But I have no clue about the abstract states, that i’d like to know too

Use /brand/1 and /brand/2 the same way you use it with /car/1 and a /car/2. :wink:

I am going to do that. The menu as an abstract state is also cached, I see no other way than disabling the cache for it.

Well, I disabled the cache for the abstract state with the menu, the only downside is that all the children states of that one are reloaded if I move to another “parent” state and then back to one state that depend on the menu.

Well, better than than no cache at all.

EDIT: No need to do that, if you don’t change your reference on the service, when I load the menu again, it will have a different value.

Curious, how did you get your car page to update with parameters? Do you have controller code you can share? My app never updates the parameter coming in for the $ionicView events to fetch more data