在执行findAll之前清除Ember's Store的数据 [英] Clear data from Ember's Store before doing findAll

查看:115
本文介绍了在执行findAll之前清除Ember's Store的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Ember 1.11
我有一个搜索功能可以运行一个参数化的find(),但是我想要能够返回到find之前的状态,只有以前的记录。在我的应用程序中,这是API返回的记录,没有查询参数。

Using Ember 1.11 I have a search function working that will run a parameterized find(), but then I want to be able to go back to the state before the find, with only the records that were there before. In my app, this is the records the API returns with no query parameters.

我的路线的模型钩子

  model: function(params) {
    if (params.q) {
      return this.store.find('project', params);
    } else {
      return this.store.findAll('project');
    }
  }

然而,目前发生的是用户回来(通过使用清除查询参数的操作):

However, what currently happens is when the user goes back (by using an action that clears the query parameter):

backToMyProjects: function() {
  this.set('q', null);
}

这个jsbin 是一个例子,从@lolmaus帮助我得到工作)
那么所有的记录仍然在商店,所以当findAll()返回两个记录集时,我真正想要的是清除该存储,然后使用findAll()中的记录。服务器API调用在两个地方都正确进行,只是模型挂钩在之后没有params 被调用,它已经被params调用了一次,那么该商店里有额外的记录。

(this jsbin is an example from @lolmaus that helped me get that working) then all the records are still in the store, so when it does findAll(), it gets back both sets of records, when what I really want is for it to clear out the store, then use the records from the findAll(). The server API call is happening correctly in both places, it's just that the model hook is called with no params after it's been called once with params, then the store has extra records in it.

所以我尝试添加 this.store.unloadAll('project'),但是从参数化查询,回到一个没有参数。

So then I tried adding this.store.unloadAll('project') but then I get an error after going from a parameterized query, back to one without parameters.

更新的模型钩子

model: function(params) {
  if (params.q) {
    return this.store.find('project', params);
  } else {
    this.store.unloadAll('project');
    return this.store.findAll('project');
  }
},
//this isn't new, just forgot to put it earlier. when the query param is modified, the model hook is called again (as I understand it).
queryParams: {
  q: {
    refreshModel: true
  } 
}

错误消息

Error while processing route: projects Assertion Failed: calling set on destroyed object Error: Assertion Failed: calling set on destroyed object
    at new Error (native)
    at Error.EmberError (http://localhost:4200/assets/vendor.js:22615:21)
    at Object.Ember.default.assert (http://localhost:4200/assets/vendor.js:15716:13)
    at Object.set (http://localhost:4200/assets/vendor.js:26367:22)
    at exports.default.mixin.Mixin.create.set (http://localhost:4200/assets/vendor.js:41034:20)
    at Ember.Object.extend.flushCanonical (http://localhost:4200/assets/vendor.js:69680:14)
    at ember$data$lib$system$relationships$state$has_many$$ManyRelationship.flushCanonical (http://localhost:4200/assets/vendor.js:71436:22)
    at Queue.invoke (http://localhost:4200/assets/vendor.js:11425:18)
    at Object.Queue.flush (http://localhost:4200/assets/vendor.js:11490:13)
    at Object.DeferredActionQueues.flush (http://localhost:4200/assets/vendor.js:11295:19)


推荐答案

else 条件中,再次使用 find()而不是 findAll()从商店获取所有东西:

In the else condition, use find() again instead of findAll() which gets everything from the store:

return this.store.find('project');

更新:没关系,在1.11以上,这样就会在cover下面调用findAll()。不知道如何强制它不使用商店。

UPDATE: never mind, in 1.11 and up, this will call findAll() under the covers anyway. Not sure how to force it to not use the store.

到目前为止,我已经将unloadAll()包装在Ember.run中,它似乎工作,但是我不知道为什么这是必要的:

so far, I have wrapped the unloadAll() in an Ember.run, and it appears to work, but I'm not sure why this is necessary:

  model: function(params) {
    if (params.q) {
      return this.store.find('project', params);
    } else {
      var _this = this;
      Ember.run(function() {
        _this.store.unloadAll('project');
      });
      return this.store.find('project');
    }
  }

这篇关于在执行findAll之前清除Ember's Store的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆