Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route with store.findAll not working correctly if records of that type already present from previous page #12488

Closed
adam-knights opened this issue Oct 16, 2015 · 6 comments

Comments

@adam-knights
Copy link

This was working fine with Ember 1.13.10 and Ember Data 1.13.12, doesn't seem to work with Ember 2.1.0 and Ember data 2.1.0.

If I navigate to http://localhost:4200/orders then it works fine and I see 6 orders come in from the api and the page displays information on all seven orders.

If I first navigate to http://localhost:4200/members/1234/orders, then I correctly see 3 orders from that member account. However, if I now press the link to goto http://localhost:4200/orders it only displays the same 3 orders. The api is deffinitely hit again and it correctly returns 6 orders.

I put a breakpoint at the end of Ember Data's _findAll method and its returning an array with 6 items from the return store.peekAll(modelName); line. So I've posted here first as I think Ember data has done its job?

This is my orders route:

import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

export default Ember.Route.extend(AuthenticatedRouteMixin, {
  model: function() {
    return this.store.findAll('order').then(function(orders) {
      return orders.sortBy('creationDate').reverse();
    });
  },
  afterModel: function(orders) {
    document.title = 'Orders';
    return Ember.RSVP.all(orders.getEach('orderItems'));
  }
});

This is my account (member) model:

import DS from 'ember-data';

export default DS.Model.extend({
  accountType: DS.hasMany('accountType', {async: true}),
  addresses: DS.hasMany('address', {async: true}),
  contacts: DS.hasMany('contact', {async: true}),
  membershipType: DS.belongsTo('membershipType', {async: true}),
  communicationsAvailable: function() {
    var commsAvailable = [];
    this.get('contacts').forEach(contact => commsAvailable.push.apply(commsAvailable, contact.get('communicationsAvailable')));

    return commsAvailable.filter(function(value, index, ca) {
      return ca.indexOf(value) === index;
    });
  }.property('contacts.@each.communicationsAvailable'),
  joinDate: DS.attr('date'),
  name: DS.attr('string'),
  orders: DS.hasMany('order', {async: true}),
  prices: DS.hasMany('price', {async: true}),
  supplierCode: DS.attr('string')
});

This is my order model:

import DS from 'ember-data';

export default DS.Model.extend({
  account: DS.belongsTo('account', {async: true}),
  address: DS.belongsTo('address', {async: true}),
  collect: DS.attr('boolean'),
  deliveryDate: DS.attr('date'),
  paymentDate: DS.attr('date'),
  memberPurchaseOrderNumber: DS.attr('string'),
  orderItems: DS.hasMany('orderItem', {async: true}),
  orderNotes: DS.hasMany('orderNotes', {async: true}),
  orderNotifications: DS.hasMany('orderNotifications', {async: true}),
  orderStatus: DS.belongsTo('orderStatus', {async: true}),
  creationDate: DS.attr('date')
});
@stefanpenner
Copy link
Member

likely something for https://github.com/emberjs/data but a reproduction in an example app would really make this actionable.

I suspect this is related to the ArrayProxy / @each issues reported lately => #12475

@adam-knights
Copy link
Author

Thanks @stefanpenner I would have posted their straight away but because findAll seemed to be returning the data I would expect I wanted a quick opinion from here first. Posted as emberjs/data#3863

@adam-knights
Copy link
Author

Ok, I'll try and put an example app together now, nearing the end of the working day here in the UK so it may be Monday that I finish and post a link.

@adam-knights
Copy link
Author

@stefanpenner here is an example app that shows it https://github.com/adam-knights/order-example-oct-2015

  1. Login (any username or password will work)
  2. Click 'Members' in the top right
  3. Click 'B company'
  4. Click 'Orders' button - you see links to orders 2, 5 and 6
  5. Click 'Orders' in the top right - you only see orders 2, 5 and 6, checking the network tab shows 6 orders came back from the api and 6 came back from findAll
  6. If you load http://localhost:4200/orders by itself then it successfully shows all 6 orders.

Let me know if you see the bug too or if you need any more information?

@adam-knights
Copy link
Author

Update: Issue still occurs in Ember 2.2.0

@adam-knights
Copy link
Author

The issue is with the background refresh behavior of ember data.

return this.store.findAll('order').then(function(orders) {
      return orders.sortBy('creationDate').reverse();
    });

FindAll resolves straight away with a background refresh then happening, changing the routes to

return this.store.findAll('order', {reload: true}).then(function(orders) {
      return orders.sortBy('creationDate').reverse();
    });

Resolves the issue.

I've opened emberjs/data#4148 to cover whether I should have got a deprecation warning about using then on findAll - as the new behavior resolves immediately if there are existing records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants