简体   繁体   English

ember-data如何重新加载/排序存储记录

[英]ember-data how to reload/sort store records

Is there a way to reload the whole store in ember-data? 有没有办法在ember-data中重新加载整个商店? Or to sort items inside? 还是对里面的物品进行排序?

It should go like that: 它应该像这样:

  • user clicks "add" button 用户单击“添加”按钮
  • new record is added 新记录已添加
  • store is commited 存储已提交
  • user is transmited to the state where items are listed 用户被传输到列出项目的状态
  • his new entry is there in correct order 他的新条目顺序正确

To do this I have to either transmit in model.didCreate which shouldn't be (it's not the role of a model!) or refresh store after transmiting. 为此,我必须要么在model.didCreate中进行传输(不应该是模型的角色),要么在传输后刷新存储。

Anyone had similar issues? 有人有类似的问题吗?

I am using ember-data revision 12 我正在使用余烬数据修订版12

EDIT 编辑

I used: 我用了:

Ember.run.later(this, function(){
    this.transitionToRoute('list');         
}, 500);

To give store some time. 给存储一些时间。 But then again new entry is always at the bottom of the list. 但是,再次,新条目始终位于列表的底部。 I still need to reload whole store or sort it somehow. 我仍然需要重新加载整个商店或以某种方式对其进行排序。

EDIT#2 编辑#2

So I did little rework here. 所以我在这里做了很少的返工。 In my template I use: 在我的模板中,我使用:

{{#each arrangedContent}}

To use sorted data and in the route I have: 要使用排序的数据并在路由中使用:

App.AdsRoute = Ember.Route.extend({
    model: function(){
        return App.Ad.find();
    },
    sortProperties: ['id']
});

It's not working though. 虽然没有用。 Looks like Route is not ArrayController. 看起来Route不是ArrayController。 And if I make additional AdsController extending from ArrayController it is not working neither. 如果我能赚到更多的AdsController延伸自ArrayController它不工作也没有。

Ember ArrayControllers have a few options for sorting the content: Ember ArrayControllers具有一些用于对内容进行排序的选项:

http://emberjs.com/api/classes/Ember.ArrayController.html#property_sortAscending http://emberjs.com/api/classes/Ember.ArrayController.html#property_sortAscending

You can set which fields to sort by and which order you want to use. 您可以设置要排序的字段以及要使用的顺序。 When you use these you will want to bind views to arrangedContent and not just plain old content. 当您使用这些内容时,您将需要将视图绑定到rangedContent,而不仅仅是普通的旧内容。

In my applications I typically return to the list state after the record has been persisted: 在我的应用程序中,记录通常会在保留后返回到列表状态:

var observer,   _this = this;

observer = function(target, path) {   
  if (target.get(path) === 'saved') {
    target.removeObserver(path, null, observer);
    return _this.get('target.router').transitionTo('apps.index');   
  } 
};

I'm not sure if the store has a reload mechanism (individual records do), but I can update this response if I find an answer. 我不确定该商店是否具有重新加载机制(单个记录有),但是如果找到答案,则可以更新此响应。

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

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