在Ember视图中,在重新渲染内部子视图后运行代码的最佳方法是什么? [英] In an Ember view, what's the best way to run code after the rerendering of an internal child view?

查看:461
本文介绍了在Ember视图中,在重新渲染内部子视图后运行代码的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多关于如何用动态数据等初始化jQuery插件的问题,通常答案是你应该使用 didInsertElement 钩子。我的问题是,当使用ember-data时,最初将视图插入为空,然后通过绑定填充模板vars。因此,在 didInsertElement 中,我需要的元素不在。

I found a lot of questions about how to initialize jQuery plugins with dynamic data etc and usually the answer is that you should use the didInsertElement hook. My problem is that when using ember-data, initially the view is inserted empty and the template vars are filled afterwards through the bindings. Thus in didInsertElement the elements I need are not there.

我发现了一个适用于我的解决方案,但我认为必须有一个更好的解决方案。

I found a solution that works for me but I think there must be a better solution.

除了 didInsertElement 方法我还观察到控制器中的属性,从那里也打电话给我的代码,包裹在一个 Ember.run.next 中,以便子视图呈现。

In addition to the didInsertElement method I also observe the attributes in the controller and call my code from there, too, wrapped in a Ember.run.next so the child view is rendered before.

有更好的解决方案来对更新的子视图做出反应?

Is there a better solution to react to updated child views?

推荐答案

这取决于你的应用程序的大小,以及您的视图呈现的元素数量。

It depends a bit on the size of your application, and the number of elements your view is rendering.

编辑:您可以成功观察RecordArray的isLoaded属性。这里有一些细节: https ://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/record_arrays/adapter_populated_record_array.js#L21

You might successfully observer the isLoaded property of the RecordArray. Some details here: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/record_arrays/adapter_populated_record_array.js#L21

我用于具有单个对象作为内容的视图的一个选项如下所示:

One option that I have used for views that have a single object as their content is something like:

MyApp.CustomView = Ember.View.extend({
    content: null,

    contentObserver: function() {
        this.rerender();
    }.observes('content')
}

如果您的视图内容是列表,则重新渲染

If your view contents is a list, it makes little sense to re render the view for each item in the list, though.

但是,我认为这种方法与您已经在做的工作非常相似。

However, I think this approach is fairly similar to what you are already doing, though.

另一种方法是使用Ember Data实现自己的适配器,这样可以告诉其余的o您的应用程序已完成加载数据。如下:

Another approach is to implement your own adapter with Ember Data. This way, you can tell the rest of your application that it's finished loading your data. Something like:

MyApp.Adapter = DS.Adapter.create({
    //Finding all object of a certain type. Fetching from the server
    findAll: function(store, type, ids) {
        var url = type.url;
        jQuery.getJSON(url, function(data) {
            store.loadMany(type, data);
        });

        //Perform some custom logic here... 
    }
}

这应该可以工作,但它不像应该/可能是通用的。

This should work, but its not as generic as it should/could be though.

另外,你可能想看看这个提交,允许注册一次性事件。

Also, you might want to take a look at this commit, which allows for registering a one-time event.

https://github.com/emberjs/ember.js/commit/1809e65012b93c0a530bfcb95eec22d972069745#L0R19

这篇关于在Ember视图中,在重新渲染内部子视图后运行代码的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆