How to notify router of property change in controller?

Say I have a search field on my controller called ‘term’. This is used to filter a list. It’s also a query param so that when the route loads it can pull the term out of the url and initialise the list from the server when the page loads given a url with a 'term" parameter in it…

ATM I am loading the data initially (when the page loads) inside the routes model hook - works great.

Filtering in the UI (typing in the ‘term’ field) sets the ‘term’ property in the controller. I have a function which does the live filtering which observes this ‘term’ property so that when it changes I can go to the server and pull the list with the ‘term’ filter applied…

I have 2 places where this fetching from the server is happening. Not good.

How can I have the filtering in only the router? Is there a way to observe the property on the controller so that I don’t need to filter inside the controller?

Is this really a problem that no one has tackled?!

Take a look at Query Parameters. In particular, the refreshModel property should be doing exactly what you want.

1 Like

Yeah - problem with refreshModel is that there is a lot of flickering and nastyness - I’ll refactor the code out into a service so it can be used in the 2 places I think :slight_smile:

I would just send an action up to the router when the term changes. I don’t really know your use case since you didn’t provide an example, but something in tune with this JS Bin - Collaborative JavaScript Debugging

Now your filtering and loading logic is on in the route… where it likely should be. If your filtering and model function are the same, then move it to another method on the route and invoke it in both places? Or, go with refreshModel like the others suggested.


can’t access that URL at work but will have a look later - it does sound more like what I need.