简体   繁体   中英

Ember view doesn't update after store.unloadAll if single item is in model

All records are unloaded from the store, but it doesn't change in view when single item is shown. If multiple items are shown, everything updates correctly.

live example: http://emberjs.jsbin.com/mojase/2/edit?html,js,output

singleItem should also disappear when all items are unloaded, but it doesn't.

That is working as intended.

find without a parameter is a live collection of all of the records in the store, and as such, when a record is removed from the store, it's removed from that collection.

find with a parameter defined is a single record (unless you're finding by query, but in this case you aren't), and Ember Data doesn't have a mechanism for retracting your reference to an object. Likewise, if you were to take the results from the find call without the parameter, and copy it into an array, then remove all of the items from the store, that array wouldn't be modified.

I do not believe a RSVP.hash is what you need:

An important note: RSVP.hash is intended for plain JavaScript objects that are just a set of keys and values. RSVP.hash will NOT preserve prototype chains. http://emberjs.com/api/classes/RSVP.html#method_hash

Your model should contain an array of objects or a single object , not both. Otherwise, you have unnecessary duplication in the model itself.

You can compare your route model to amazon: 'I do not know why you need all this stuff, but you ordered it. I promise you I will deliver them. So wait until the package arrives. Then you can filter, order, modify, .. your stuff however you like!' .

Your page logic goes into your ApplicationController , it's like your home, where your amazon stuff arrives and you can use it however you like.

So in code:

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return this.store.find("post");
  }
});

App.ApplicationController = Ember.ArrayController.extend({
 singlePost: function() {
   return this.get('model').findBy('id', '1');
 }.property('model.@each.id'),
  actions: {
    unload: function(){
      this.store.unloadAll("post");
    }
  }
});

Note that I use an ArrayController because I am returning an array of records. Use a Controller when only 1 record is returned via your model.

Working jsbin: http://emberjs.jsbin.com/zasorasido/1/edit?html,js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM