1
votes

I have a page with a search bar. Upon entering text and clicking enter, a transition occurs to the same page with a query in the URL (ie .../search/banana). Due to the way the model and setupController hooks fire, I have set up my code as follows:

model: Updates the search text field with the text that was passed, /and changes the controller's model to the current JavaScript timestamp as a hack to make sure Ember calls setupController/.

setupController: obtains the text from the search field, and should then update the model with the proper search results.

What I'm doing in the model hook is a hack, but I'm not sure how else to do this in a way that remains consistent with my URL requirement (the search should work whether somebody manually enters an appropriate URL, or a transitionTo occurred)

I'd appreciate it if somebody could tell me if there's a "right" way to ensure that setupController is called regardless of whether or not Ember thinks that the model has changed (which seems to be the culprit that's currently necessitating the hack.)

2

2 Answers

0
votes

I don't use setupController myself, but if the search bar is used on entire application, you should define this on ApplicationController. If not, you can define it in your controller.

Application template:

{{view Ember.TextField valueBinding="searchKeyword" action="doSearch"}}

App.js:

App.ApplicationController = Ember.Controller.extend({
    searchKeyword: '',
    actions: {
        doSearch: function()
        {
           var keyword = this.get('searchKeyword');
           // do your logic here
        }
    }
});

When user hit enter, it will trigger doSearch action and you can just

this.get('model').filter()

or any logic you want.

0
votes

@dgbonomo, I think your existing approach is great. From an Ember point of view your model is changing each time that a new search happens. Think of the query as the main model, and the search results as a collection of models that "belong" to the query.