当使用DOM更新完成垃圾时执行某些操作 [英] Do something when ember is done with DOM updates

查看:89
本文介绍了当使用DOM更新完成垃圾时执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经尝试使用来自操作绑定的函数的回调函数在执行回调时,DOM不会被更新。



我直接在模型上尝试了一个观察者,当执行观察者时,DOM不会更新。 >

我已经尝试了一个观察者的绑定,当执行观察者时,DOM不会被更新。



eg

  App.view = Ember.View.extend({
modelBinding:'App.model',
modelChanged :function(){
window.scrollTo(0,document.body.scrollHeight);
} .observes('model'),

getMore:function(event){
App.set('model',somethingnew);
}
});

当我点燃gotMore时,我更新了模型,当模型更新并且更改已经呈现,我想向下滚动。



在我尝试过的任何方式中,我都能够获得新的scrollHeight。在这些事件之后,它设置了几ms。



这是一个jsFiddle的例子:
http://jsfiddle.net/kcjzw/15/

解决方案

正确的方法是记录在这里:



http://emberjs.com/api/classes/Ember.run.html#method_next

  modelChanged: function(){
Ember.run.scheduleOnce('afterRender',this,function(){
window.scrollTo(0,document.body.scrollHeight);
$('body' ).append('New scroll height:'+ document.body.scrollHeight);
});
} .observes('content')


I want to do stuff when ember bindings have synchronized and the DOM is again up to date.

I have tried with a callback from the function that manipulates the binded model, DOM is not updated when callback is executed.

I have tried with an observer directly on the model, DOM is not updated when the observer is executed.

I have tried with an observer on the binding, DOM is not updated when the observer is executed.

e.g.

App.view = Ember.View.extend({
    modelBinding: 'App.model',
    modelChanged : function() {
        window.scrollTo(0, document.body.scrollHeight);
    }.observes('model'),

    getMore: function(event) {
        App.set('model', "somethingnew");
    }
});

When I fire the "gotMore", I update the model, and when the model is updated and its changes have been rendered I want to scroll down.

In none of the ways I've tried have I been able to get the new scrollHeight. It is set a few ms after these events.

Here's an example on jsFiddle: http://jsfiddle.net/kcjzw/15/

解决方案

The correct way to do this is documented here:

http://emberjs.com/api/classes/Ember.run.html#method_next

modelChanged : function() {
  Ember.run.scheduleOnce('afterRender', this, function() {
    window.scrollTo(0, document.body.scrollHeight);
    $('body').append('New scroll height: '+document.body.scrollHeight);
  });
}.observes('content')

这篇关于当使用DOM更新完成垃圾时执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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