/users
我的余烬应用程序中有一个URL。我RESTAdapter
用来从服务器获取数据。当users
加载显示列表的模板时,它会从服务器加载最新的更新数据(例如,如果我在users表的一行中进行更改,例如更改用户名,则该更改会反映在前端以及)。但是,如果我从数据库中删除该用户,那么该用户仍会在前端的用户列表中。
用于用户路由的模型挂钩仅返回服务器中的用户列表:
this.store.find('user').then(onSuccess, onError);
当我尝试对已删除的用户执行任何操作时(例如在前端更新其名称),我得到一个错误(这很明显,因为该用户不再在数据库中,并且服务器以适当的错误进行响应)。如何强制emberjs加载数据库中存在的用户列表,而不仅限于本地ember数据存储中?此外,为什么本地余烬数据存储与数据库同步以进行更新(以及添加),但不进行删除?
该问题在github问题上进行了解释。
商店为每种模型类型都有一个缓存,然后当商店使用_findAll方法方法获取数据时,它不假定正在返回所有数据,因此将应用以下步骤
store.pushMany(type, payload); // push the server response data to the local store
store.didUpdateAll(type); // update record with the most up-to-date record
return store.all(type); // return all the record in the local store
它将不考虑本期中讨论的任何记录删除,在这种情况下,如果找不到具有相同primaryKey的记录,则新的响应数据将被添加到本地存储中;否则,它将更新本地副本。
我创建了一个测试来检查定义的行为:
test('store.findAll不管理记录删除',function(){
$ .mockjaxClear();
$ .mockjax({url:'/ categories',dataType:'json',responseText:{
categories:[ {id: 1, name: "Name1"}, {id: 2, name: "Name2"} ]}});
var store = this.store();
停();
store.find('category')。then(function(result){equal(result.get('length'), 2, 'the new item returned with ajax is added to the local store'); $.mockjaxClear(); $.mockjax({ url: '/categories', dataType: 'json', responseText: { categories:[ {id: 3, name: "Name3"}, ]}}); store.find('category').then(function(result) { equal(result.get('length'), 3); $.mockjaxClear(); $.mockjax({ url: '/categories', dataType: 'json', responseText: { categories:[ {id: 3, name: "Name4"}, ]}}); store.find('category').then(function(result) { equal(result.get('length'), 3); equal(result.objectAt(2).get('name'), 'Name4', 'the old item returned with the new ajax request updates the local store'); start(); }); });
});
});
正如您指出的那样,诸如find('modelName',{})之类的东西可以解决您的问题:
this.store.find('user', {})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句