采用Ember.run.next
https://github.com/emberjs/ember.js/blob/master/packages/ember-metal/lib/run_loop.js#L531-566
App.view = Ember.View.extend({
modelBinding: 'App.model',
modelChanged : function() {
Ember.run.next(myContext, function(){
// code to be executed in the next RunLoop, which will be scheduled after the current one
window.scrollTo(0, document.body.scrollHeight);
});
}.observes('model'),
getMore: function(event) {
App.set('model', "somethingnew");
}
});
更新
看看这个:http: //jsfiddle.net/ud3323/hZ7Vx/
您需要的是在渲染助手将创建Ember.CollectionView
的runloop 之后运行您的代码。{{each}}
JavaScript:
App = Ember.Application.create();
App.model = Ember.Object.create({
items: [1]
});
App.view = Ember.Handlebars.EachView.extend({
contentBinding: 'App.model.items',
itemViewClass: Ember._MetamorphView.extend({
templateName: 'the_template'
}),
modelChanged : function() {
Ember.run.next(this, function(){
window.scrollTo(0, document.body.scrollHeight);
$('body').append('New scroll height: '+document.body.scrollHeight);
});
}.observes('content'),
theAction: function(event) {
App.controller.doStuffToModel();
}
});
App.controller = Ember.Object.create({
doStuffToModel : function() {
App.model.set('items', [1,2,3,4,5]);
}
});
车把:
<script type="text/x-handlebars" data-template-name="the_template">
<div style="height:200px;"></div>
</script>
<script type="text/x-handlebars">
{{view App.view}}
</script>