javascript - 如何在渲染模板后触发功能

标签 javascript templates backbone.js marionette

我在我的应用程序中使用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/

相关文章:

java - 在没有 NodeJS 的情况下使用 EJS

javascript - backbone js 对我的联系人列表进行排序

javascript - 在 1 行上有两个元素,但保持不同的高度

javascript - 你如何阅读这个 JavaScript 正则表达式?

javascript - Nightwatch 执行()不是预期后的方法

javascript - 定义 DOCTYPE 时 XSLT 和 javascript 的奇怪行为

c++ - 模板别名和相关名称

c++ - 在不同的命名空间中测试特定的类名(SFINAE?)

javascript - this.$el.on Backbone.Marionette

javascript - 'cache: false' 是否阻止缓存或对请求进行 UNIQUE-IFY 以绕过缓存?