简体   繁体   English

如果单个项目在模型中,则在store.unloadAll之后,Ember视图不会更新

[英]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 实时示例: http : //emberjs.jsbin.com/mojase/2/edit?html,js,output

singleItem should also disappear when all items are unloaded, but it doesn't. 当所有项目都卸载后,singleItem也应该消失,但事实并非如此。

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没有参数的情况下, find是商店中所有记录的实时集合,因此,当从商店中删除记录时,该记录也会从该集合中删除。

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. 具有定义的参数的find是一条记录(除非您通过查询找到,但在这种情况下不是),并且Ember Data没有用于撤消对对象的引用的机制。 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. 同样,如果要从不带参数的find调用中获取结果,并将其复制到数组中,然后从商店中删除所有项目,则该数组将不会被修改。

I do not believe a RSVP.hash is what you need: 我不相信您需要RSVP.hash

An important note: RSVP.hash is intended for plain JavaScript objects that are just a set of keys and values. 重要说明:RSVP.hash适用于纯JavaScript对象,这些对象只是一组键和值。 RSVP.hash will NOT preserve prototype chains. RSVP.hash不会保留原型链。 http://emberjs.com/api/classes/RSVP.html#method_hash http://emberjs.com/api/classes/RSVP.html#method_hash

Your model should contain an array of objects or a single object , not both. 您的模型应包含一个array of objects或一个single object ,不能同时包含两者。 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. 您可以将您的route model与亚马逊进行比较: “我不知道为什么需要所有这些东西,但是您订购了它。 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. 您的页面逻辑进入了ApplicationController ,就像您的家一样,您的亚马逊资料到达了这里,您可以随意使用它。

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. 请注意 ,我使用ArrayController是因为我要返回一个记录数组。 Use a Controller when only 1 record is returned via your model. 通过模型仅返回1条记录时,请使用Controller

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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