gpt4 book ai didi

ember.js - Ember Data : CreateRecord not updating model when using store. 使用查询参数查找

转载 作者:行者123 更新时间:2023-12-01 12:35:37 25 4
gpt4 key购买 nike

当我的路线模型使用 this.store.find('user') 时它会自动更新并且模板显示使用 createRecord 创建的新记录.所以下面的工作按预期进行:

路线:

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

模板:

{{#each user in model}}
{{user.username}}<br>
{{/each}}

Controller 代码:

  // Triggered when form submitted to create a new user
var newUser = this.store.createRecord('user', {
username: this.get('username').trim()
});
// new user shows up in template immediately after createRecord

如果我更改路由模型以使用 find 的查询参数版本当我执行 createRecord 时,模板不再更新

路线:

  model: function(){
// query params version of store.find
return this.store.find('user', {enabledOnly: false, limit: 100});
}

Controller 代码:

  // Triggered when form submitted to create a new user
var newUser = this.store.createRecord('user', {
username: this.get('username').trim()
});
// new user does not show up in template at all

这似乎是一个错误,因为代码中唯一的变化是从基本的 find('user') 切换。到具有查询参数的版本。这是 ember 数据的预期行为吗?为什么模型在 createRecord 之后不更新模板?当使用 find 的查询参数版本时调用(即 find('user', {}) )

我能够创建一个 jsbin 来演示这个问题。

http://jsbin.com/kilaridoso/2/edit?html,js,output

谢谢!

我正在使用以下版本:

DEBUG: -------------------------------
ember.debug.js:5197DEBUG: Ember : 1.11.1
ember.debug.js:5197DEBUG: Ember Data : 1.0.0-beta.16.1
ember.debug.js:5197DEBUG: jQuery : 1.11.3
ember.debug.js:5197DEBUG: Ember Simple Auth : 0.8.0-beta.2
ember.debug.js:5197DEBUG: -------------------------------

最佳答案

认为这是我在 Ember-Data 的 GitHUB 页面上发布的错误。提供的答案是这是预期的行为。这是 wecc 的完整回复(谢谢!)

Using store.find(type, query) does not return a live RecordArray so the behavior you're describing is correct.

You should be able to use store.filter(type, query, filter) (docs) instead.

The reason for store.find(type, query) not updating with the newly created record is that query is just sent to the server, there's no way for ED to know if new records "match" that query or not. Sometimes the query might be a simple filter like { isUnread: true } but it can also be something like { since: '2015-05-10 10:51' }. Using store.filter(type, query, filter) on the other hand, passing both query and a filter callback function, ED can pass query to the server and use the filter function on all new records to know if it's to be included in the live RecordArray or not.

这是答案的链接:

https://github.com/emberjs/data/issues/3057

关于ember.js - Ember Data : CreateRecord not updating model when using store. 使用查询参数查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30147249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com