将.CTP视图的设置变量设置为.js



我有一个非常烦人和无意的问题,您准备好了吗?:x

好吧,我们去:

实际上,我有一个特定的软件部署到4个环境中:开发,认证,制作和生产。

昨天我收到了生产环境中的错误报告。

事实是,当我尝试将一个从我的.ctp设置为.js我的.js我的.js无法访问该变量时,因此未完成"集合"。奇怪的是,在其他环境(DES,CERT和PRE)中,可以很好地工作,但在Pro中(对于某种casuistic而言,它失败了)。

绘制问题:

  • translate.ctp

    <?php
    $translations = array();
    $translations['NO_DATA']=__('NODATA');
    $translations['VALUE']=__('VAL');
    $translations['WEBROOT'] = $this->webroot;
    $this->Js->set('translations',$translations); // <-- prepare 'translations' into JS as window.app.translations
    
    echo $this->Js->writeBuffer(array('onDomReady' => false)); // sets the variable 'translations' into the javascript (I only use writeBuffer once in this code and project)
    ?>
    
  • default.ctp

...标题,脚本加载和东西...

<?php echo $this->element('translate');?> // loads translations ctp
<?php echo $this->fetch('script'); ?>
  • main.js

     $(document).ready(function() {
                // window.app doesn't exists so the following instruction will trigger an error:
                var translatedNoData = window.app.translations['NO_DATA'];
     }
    

非常感谢您,希望上面的信息足够了。

我正在更新此帖子,以解决上面发布的'问题。

上面的代码还可以。 $ this-> js-> set .. and $ this-> js-> js-> writebuffer 是从.ctp将变量直接设置为.js的正确方法作为全局变量(它将在window.app内部设置)

问题是由用户插入的数据引起的。他将一些"不将其关闭的db表中的一列中关闭,这些内容直接渲染到HTML DOM元素中,因此它粉碎了接下来的所有内容...

sry for the Mildussanding:)

最新更新