简体   繁体   English

使用Ember和Ember数据:如何在存储加载完成后和/或关联视图完成重新渲染时设置回调?

[英]With Ember and Ember-data: How to setup callback when a store is finished loading, and/or when an associated view is finished re-rendering?

I'm using Ember and Ember-data to load a few hundred objects from a REST API, using a findAll call within a custom adapter. 我正在使用Ember和Ember数据从REST API加载几百个对象,使用自定义适配器中的findAll调用。 I have an ArrayController.content referencing the findAll, and then use the recently added Ember.Select to display the objects in a select widget/dropdown-menu. 我有一个引用findAll的ArrayController.content ,然后使用最近添加的Ember.Select在select小部件/下拉菜单中显示对象。 I need to run a function on the select widget once it is fully rendered with all the objects (each object being an option of the select) - in particular, the Chosen.js library . 一旦完全呈现所有对象(每个对象都是select的选项),我需要在select小部件上运行一个函数 - 特别是Chosen.js库

Because it takes a little while (2-4 seconds) to handle the few hundred objects in the select, using callbacks on the events Ember.Select.didInsertElement and Ember.ArrayController.contentDidChange don't quite work; 因为处理select中的几百个对象需要一些时间(2-4秒),所以在事件Ember.Select.didInsertElement和Ember.ArrayController.contentDidChange上使用回调不太有用; they both fire too soon. 他们都很快开火。 So is there another event, or another approach, which could be used instead? 那么是否可以使用另一种事件或另一种方法呢?

DS.RESTAdapter.findQuery is the answer! DS.RESTAdapter.findQuery就是答案! In contrast to the DS.RESTAdapter.findAll method, it creates and returns DS.AdapterPopulatedModelArray , which has it's own isLoaded properly that you can observe anywhere in your app! DS.RESTAdapter.findAll方法相比,它创建并返回DS.AdapterPopulatedModelArray ,它具有正确的isLoaded ,您可以在应用程序的任何位置观察!

For example: 例如:

App.store = DS.Store.create({
    adapter: DS.RESTAdapter.create()
});

App.set('MyItemList', App.store.findQuery(App.Item, 'homepageList'));

App.MyView = Ember.View.extend({
    refresh: function () {
        console.log('finished loading custom list');
    }.observes('App.MyItemList.isLoaded')
});

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

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