【问题标题】:Refresh plural route model when new record is saved保存新记录时刷新复数路径模型
【发布时间】:2015-08-06 22:56:18
【问题描述】:

我的路线如下所示:

model() {
  return this.store.query('my-model', {
    offset: this.get('offset'),
    limit: this.get('limit')
  })
}

我在/my-models 这样的路由中使用它,并渲染模型列表。

然后,在/my-models/new 这样的路线中,我制作并保存了一个新模型。但是,当我保存新模型时,模型列表不会更新以反映新模型的新存在。

我认为这是因为store.query 不监控商店,它只从服务器获取。但是,当我尝试 store.findAll(我认为它应该监视存储的更改)时,它会返回 每条 记录,而不仅仅是偏移/限制参数之间的记录。

有什么方法可以保持分页工作,同时将新创建的记录自动添加到列表中?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    您可以在保存新模型后使用推送方法。像这样的:

    var store = this.get('store');
    var newModel = store.createRecord('my-model');
    newModel.set('param', 'param value')
      .save()
      .then(function () { store.push('my-model', newModel); })
    

    文档:http://emberjs.com/api/data/classes/DS.Store.html#method_push

    【讨论】:

      【解决方案2】:

      我不确定这个,但如果您使用的是 Ember Data 1.13,您可以尝试使用新的重新加载设置。

      findAll 的文档(我知道这不是你想要的)说:

      store.findAll('user');  //goes to the server the first time
      store.findAll('user');  //after that returns from cache, but updates in background
      store.findAll('user', { reload: true });  //enforces getting fresh data
      store.findAll('user', { backgroundReload: false });  //opts out of background updating
      

      如果您尝试 backgroundReload: truequery(作为第三个参数)会在后台刷新查询吗?

      【讨论】:

      • 似乎没有任何改变 :(
      猜你喜欢
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多