2

I can work with Ember.js(rc0) and Rails, and have a simple app working as I'd expect, but want to focus on a specific story:

As a user, I want to type in "filter" text in a form, then have my ArrayController only show me those items that match the filter. For example, think of a Contacts app that shows people with the name like "Ya%"...

Caveat: Say the database holds thousands of Contact records. I don't want to filter those contacts on the client, it makes more sense to do that on the server.

Question: How do I do this in ember.js/ember-data? On the server, I can easily allow for a search parameter in my index URL to filter data so it's a manageable list, or even limit the response to say, 20 items.

I can also use a view to have access to my filter text in my controller, but where do I go next? How can I pass that filter onto the server?

Update:

I was able to use "find" on the model object, and ember (ember data) went to the server to grab new data - as the client side only had a subset of all the Contact records to begin with. Instead of filtering on what on the client, it automatically deferred to the the server... which is nice.

App.ContactIndexController = Ember.ArrayController.extend
 search_term: null
 submit: (view) ->
   this.set('content', App.Contact.find({search: "#{view.search_term}"}))
Daniel D
  • 3,637
  • 5
  • 36
  • 41

1 Answers1

2

This is a good use case for findQuery. For example:

store.findQuery(App.Contact, {q: queryString})

This will in turn call findQuery on the appropriate adapter, and if successful, load the returned records into the store and return a DS.AdapterPopulatedRecordArray.

Note that you can completely customize the query object to include params that match your server's endpoints.


Update: As Michael pointed out in the comments, the above is equivalent to:

App.Contact.find({q: queryString})

... which is certainly a cleaner solution, especially without direct access to the store.

Dan Gebhardt
  • 3,251
  • 1
  • 18
  • 15
  • Hmm - How do I get a handle on that store object? For example, I'll want to access that information in a controller after a user defines a text filter... – Daniel D Feb 26 '13 at 21:24
  • Might that api (findQuery) be gone in the RC? I was able to use "find" on the controller, and that went back to the server database: this.set('content', App.Contact.find({search: "#{view.search_term}"})), where I support a search param in my server controller... – Daniel D Feb 26 '13 at 21:39
  • Thanks @MichaelGrassotti - that is the more conventional way to call findQuery now. I just updated my answer. – Dan Gebhardt Feb 26 '13 at 22:56