emberjs/data — pull request #185 by rlivsey (2012-04-05 10:21:02+00:00)
Title: WIP - register DS.AdapterPopulatedModelArray as a result of findQuery an...

...d allow passing in a filterFunction to filter subsequent additions.

--

As per #183, this registers the ModelArray generated from DS.Store#findQuery so that it's notified of updates just like a findAll.

It extends FilteredModelArray so that it can take a filterFunction, which defaults to returning false and so rejecting new records unless you explicitly set one up.

If no filterFunction is provided, we could look at the query hash and attempt to apply that, for example:

App.store.find(App.ChatMessage, {project_id: 123})

Could be equivalent to:

App.store.find(App.ChatMessage, {project_id: 123}, function(record) {
return record.get("projectId") == 123;
})

Obviously it would need to run the query hash through NamingConvention for that to work.

The main problem at the moment is that the object passed to the filterFunction isn't actually the record object. It's an object which has the record data in savedData when data is initially loaded, and then later a DS._DataProxy object with a record property.

That means in my example app the filter is currently along the lines of:

App.store.find(App.ChatMessage, {project_id: 123}, function(proxy) {
var record;
if (record != proxy.get("record")) {
return record.get("projectId") == 123;
}
})

This works, but obviously isn't an interface which would want to be public!

I've not added any new tests, but was surprised to see that it didn't break any existing ones! Mainly looking for feedback as to whether this is a worthwhile angle of investigation and worth continuing development.