Sarus
Sarus

Reputation: 3313

Ember Data: CreateRecord not updating model when using store.find with query params

When my route model uses this.store.find('user') it updates automatically and the template shows new records created using createRecord. So the following works as expected:

Route:

  model: function(){
    return this.store.find('user');
  }

Template:

{{#each user in model}}
      {{user.username}}<br>
{{/each}}

Controller Code:

  // Triggered when form submitted to create a new user
  var newUser = this.store.createRecord('user', {
    username: this.get('username').trim()
  });
  // new user shows up in template immediately after createRecord

If I change my route model to use the query params version of find the template no longer updates when I do a createRecord

Route:

  model: function(){
    // query params version of store.find
    return this.store.find('user', {enabledOnly: false, limit: 100});
  }

Controller Code:

  // Triggered when form submitted to create a new user
  var newUser = this.store.createRecord('user', {
    username: this.get('username').trim()
  });
  // new user does not show up in template at all

It seems like this might be a bug as the only change in the code is switching from the basic find('user') to the version that has query params. Is this expected behavior for ember data? Why would the model not update the template after createRecord is called when the query param version of find is used (i.e., find('user', {}))

I was able to create a jsbin that demonstrates the issue.

http://jsbin.com/kilaridoso/2/edit?html,js,output

Thank you!

I am using the following version:

DEBUG: -------------------------------
ember.debug.js:5197DEBUG: Ember             : 1.11.1
ember.debug.js:5197DEBUG: Ember Data        : 1.0.0-beta.16.1
ember.debug.js:5197DEBUG: jQuery            : 1.11.3
ember.debug.js:5197DEBUG: Ember Simple Auth : 0.8.0-beta.2
ember.debug.js:5197DEBUG: -------------------------------

Upvotes: 3

Views: 1725

Answers (1)

Sarus
Sarus

Reputation: 3313

Thinking this was a bug I posted over on Ember-Data's GitHUB page. The answer provided is that this is expected behavior. Here is the full response from wecc (thanks!)

Using store.find(type, query) does not return a live RecordArray so the behavior you're describing is correct.

You should be able to use store.filter(type, query, filter) (docs) instead.

The reason for store.find(type, query) not updating with the newly created record is that query is just sent to the server, there's no way for ED to know if new records "match" that query or not. Sometimes the query might be a simple filter like { isUnread: true } but it can also be something like { since: '2015-05-10 10:51' }. Using store.filter(type, query, filter) on the other hand, passing both query and a filter callback function, ED can pass query to the server and use the filter function on all new records to know if it's to be included in the live RecordArray or not.

Here is a link to the answer:

https://github.com/emberjs/data/issues/3057

Upvotes: 4

Related Questions