The right way to navigate ToRoute

I am doing a direct search on ember.js. This is the code

App.Router.map -> @resource "index", {path : "/"} @resource "index", {path : "/:query"} App.Torrents = findByQuery : (query) -> url = "/api/find/#{query}" $.getJSON(url) App.IndexRoute = Ember.Route.extend model : (params) -> App.Torrents.findByQuery(params.query) App.IndexController = Ember.ArrayController.extend onChangeQuery : _.debounce(-> query = @get("query") @transitionToRoute("index", {query : query}) , 500).observes("query") 

I have a query property related to input. When changing the input, I want to go to the route that passes the new query parameter, but the IndexRoute.model method is not called.

+6
source share
2 answers

The reason for the IndexRoute.model method IndexRoute.model not called. this is

A route with a dynamic segment will only have a model hook called when it is entered through the URL. If a route is entered through a transition (for example, when using the link-to Handlebars helper), then the model context is already provided and the hook is not executed. Routes without dynamic segments will always hook the model.

explained here .

So, as discussed in this issue , use the setupController hook to retrieve your model in these cases.

Run the bin of your code with setupController

+9
source

It’s a pity that I am late, and it may be useless to you. I just wanted to post it here if this might be helpful to others.

This link helped me clear my problem.

Approach 1: We can provide a model for the route. The model will be serialized to the URL using the route serialization hook:

 var model = self.store.find( 'campaign', { fb_id: fb_id } ); self.transitionToRoute( 'campaign', model); 

This will work fine for routing, but the url can be changed. For this case, we need to add additional logic to serialize the object passed to the new route and to fix the URL.

Approach 2: If a literal (such as a number or a string) is passed, it will be treated as an identifier instead. In this case, the hook of the route model will be launched:

 self.transitionToRoute( 'campaign', fb_id); 

This will call the model () and correctly display the required URL when routing. setupController () will be called immediately after the model ().

2nd worked fine for me. Hope this is helpful and answers the above question.

+2
source

Source: https://habr.com/ru/post/953200/


All Articles