【问题标题】:How to trigger function after render template渲染模板后如何触发函数
【发布时间】:2014-10-18 13:30:33
【问题描述】:

我在我的应用程序中使用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 and data 作为参数传递给commonFunctions.templateCompilation。它将编译并返回compiled string。编译结果传递给template

根据我的假设,template 完成后,onRender 函数将触发。我在onRender 之前的意思是,无论我们使用template 模板化的内容,dom 都将可用。

但我在onRender 函数中得到了null

我想要一个回调,它应该在 template 在 dom 中可用之后触发。所以我可以访问使用template 模板化的任何元素。

我可以做一件事,无论我在onRender 里面写什么,我都可以像下面这样设置time

 onRender:function(){
    setTimeout(function(){console.log(document.getElementById("productInfoBtn"));},1000);
 }

如果我设置time,工作正常,但它不是正确的实现方式。

谁能帮帮我。

谢谢。

【问题讨论】:

    标签: javascript templates backbone.js marionette


    【解决方案1】:

    已解决,我必须使用onShow 而不是onRender 函数。现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 2014-10-10
      • 2016-11-13
      • 2017-09-10
      相关资源
      最近更新 更多