我在我的应用程序中使用marionette
。我通过 regions
显示 ItemView
,如下所示。
var productInfoViewObj=new productInfoView.ProductInfoView({model:tagInformationModel.tagInformationModelObj});
exports.MyApp.bodyContainer.show(productInfoViewObj);
这是我在view
内编写的代码。
exports.ProductInfoView=Backbone.Marionette.ItemView.extend({
domInfo:{
mainTemplateId:"tagProductListTpl",
tableTemplateId:"taginfoViewTpl",
tableContentDiv:"taginfoViewDiv",
//tad Info
tagInfoTabId:"tagInfoBtn",
productInfoTabId:"productInfoBtn"
},
template:function(){
return commonFunctions.templateCompilation("tagProductListTpl","");
},
onRender:function(){
console.log(document.getElementById("productInfoBtn"));
}
});
我将 templateId 和 data
作为参数传递给 commonFunctions.templateCompilation
。它将编译并返回编译后的字符串
。编译结果传递给模板
。
根据我的假设,template
完成后,onRender
函数将触发。我的意思是,在 onRender
之前,无论我们使用 template
进行模板化,dom 都将可用。
但是我在 onRender
函数中得到了 null
。
我想要一个回调,它应该在 dom 中的 template
可用后触发。这样我就可以访问使用 template
模板化的任何元素。
我可以做一件事,无论我在 onRender
中编写什么,我都可以像下面这样设置time
。
onRender:function(){
setTimeout(function(){console.log(document.getElementById("productInfoBtn"));},1000);
}
如果我设置时间
,工作正常,但这不是正确的实现方式。
谁能帮帮我。
谢谢。
最佳答案
已解决,我必须使用 onShow
而不是 onRender
函数。现在一切正常。
关于javascript - 如何在渲染模板后触发功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440269/