我目前正在使用 emberjs 构建一个应用程序,即将完成,但与应用程序中的 View 相关的 didInsertElement 方法存在一些不一致。 这是代码:
didInsertElement : function(){
Ember.run.next(this,function(){
var sub = this.$(document).find('.sub_t');
$(sub).not('button').siblings('.inside').toggle();
$(sub).each(function(i){
$(this).attr('id','s_t' + i + '');
var s_t = $(this).nextUntil(".sub_t");
$(s_t).not('button').wrapAll("<div class='s_t" + i + "'></div>");
});
});
},
问题是,即使我没有使用 Ember.run.next,应用程序 View 的 react 也不一致。我只是想修改我认为的内容。有时 jquery 可以工作,有时却不能,并且不清楚如何重现此错误。
与加载的文件大小有关吗? 或者是 Ember 运行循环或 did 插入元素方法的误用?
有人对此有线索吗?或者提示正在发生的事情以及我可以采取哪些措施来解决这个问题?
最佳答案
当调用 didInsertElement
时,您可以保证 View 的元素已添加到 DOM 中。如果您还想保证 View 的 subview 也已添加,您可以在 afterRender
队列上安排一个操作,该队列已添加 here 。即
Ember.run.scheduleOnce('afterRender', this, function(){
// do something when my view hierarchy has been rendered
});
这将取代 Ember.run.next
,这在 didInsertElement
处理程序中很少是一个好主意。
此外,在 didInsertElement
处理程序中,您几乎应该始终使用作用域 jQuery 选择器 this.$()
,而不是全局 $
.
在这两个提示之间,您应该能够使事情保持一致。如果不能,您可以创建一个 JSFiddle,显示您要完成的任务的简化版本,我很乐意查看它。
关于javascript - 使用didInsertElement时与emberjs和jquery不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13863195/