genkgo / ember-localforage-adapter

Offline usage for Ember Data, based on localstorage adapter, but now uses Mozilla's localforage as data source
Other
133 stars 26 forks source link

Routes not loading updated attributes #28

Closed zshnr closed 9 years ago

zshnr commented 9 years ago

I’m prototyping a store front. I’m loading all the product model data by pushing it to localstorage (using the localforage adapter) in the application route when the app starts for the first time:

adapters/application.js

import LFAdapter from 'ember-localforage-adapter/adapters/localforage';

export default LFAdapter.extend({
   shouldReloadAll: function() {
       return true;
    }
 });

routes/application.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    this.store.push({
      data: {
        id: '0',
        type: 'product',
        attributes: {
          "name": "Shoes",
          "price": 100,
          "quantityInStock": 2,
          "isOnSale": false
        }
      }
    });
});

Now, on the index route I change the attribute (like say isOnSale) by pressing a button that saves it to localstorage using .save()

The changes persist just fine on the index route, but when I goto another route (say /sale), the isOnSale that is shown is the old one that was pushed in the initialization of the app, not the updated one from store.

The code for the other route is:

routes/sale.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
      return this.store.filter('product', function(product) {
          return product.get('isOnSale');
      });
  }
});

Is this a problem with the adapter?

frederikbosch commented 9 years ago

@zshnr I believe shouldReloadAll is a method from Ember Data 1.13. This library is not compatible yet with this version. You can find a branch where I started making it compatible, but that was before the recently published blogpost on the changes made in 1.13.

With that blogpost it should be more clear how to upgrade the adapter. If you would like to help with the compatibility, please go ahaed :)

zshnr commented 9 years ago

@frederikbosch ah, I had a feeling that is what was breaking. Thanks a lot for the clarification :)

I would love to help you with this, unfortunately I'm not only a junior Ember developer, I'm a junior developer. I have no idea where to start helping you even though I would really like to.

zshnr commented 9 years ago

@frederikbosch So I changed ember-data to 1.0.0.beta-18 and still the same issue. (I removed the shouldReloadAll hook from adapter.js the rest of the code is still the same.

I noticed though that this problem is with using the filter method. If I use the find method, then the data loaded on the other route is the updated data.

Is there any way I can filter data without using the filter method? thanks a lot!

frederikbosch commented 9 years ago

@zshnr Yes there is. From the top of my head.

export default Ember.Route.extend({

  setupController:  function (controller, model) {
    this._super(controller, model);
    controller.set('filtered', model.filterBy('isOnSale', true));
  },

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

});
frederikbosch commented 9 years ago

@zshnr I will have a look at the filter method. Which version of Ember are you using? Be aware of the fact that filter will be deprecated in Ember 2.0.0 and moved to an addon.

zshnr commented 9 years ago

@frederikbosch I downgraded to: ember: 1.12.0 & ember-data 1.0.0.beta18. Thanks a lot for checking it out & the heads up on the impending deprecation :)

zshnr commented 9 years ago

@frederikbosch missed your previous post completely, let me try that. Thanks!

Edit:

Tried it out. It may need to be tweaked a bit because it was returning all the items in the product model. However, I found that this worked:

export default Ember.Route.extend({
    model: function() {
         var store = this.store;
         return store.find('product').then(function() {
               return store.filter('product', function(product) {
                  return product.get('isInCart');
               });
         });
   }
});

Thanks a lot for your help! :)

frederikbosch commented 9 years ago

@zshnr No problem. Will leave this issue open until I have created a test that shows the problem or proves that the problem must on your side.

frederikbosch commented 9 years ago

@zshnr The issue on the store.find method, is that still Ember Data 1.13? Or did your downgrade to a 1.0 beta x?

zshnr commented 9 years ago

store.find works on 1.0.beta.18. But it breaks in 1.13.2 (that is the one I tried). It was store.filter that was still breaking in 1.0.beta.18

frederikbosch commented 9 years ago

@zshnr The behaviour of the adapter is correct for store.filter in 1.0 beta 18. Do not forget to pass a query, otherwise Ember Data will not ask the adapter to lookup records.

Since there is already an issue on Ember Data 1.13, I will close this one.