我的CakePHP项目中有几个js文件,它们需要一个用于ajax调用的路径。我在服务器上生成路径,并希望以一种通用的方式将它们传递给javascript。我不想将它们作为参数添加到函数中,因为它们是静态的。现在我这样做:
<?php
$categories = Router::url(array('controller' => 'categories', 'action' => 'getChildCategories'));
$brands = Router::url(array('controller' => 'brands', 'action' => 'autoComplete'));
// first add the variables
echo $this->Html->scriptBlock(
'var CATEGORIE_GETSUBCATEGORY = "' . $categories . '";
var BRAND_AUTOCOMPLETE = "' . $brands . '";',
array('inline' => false)
);
// then include the file that uses them
echo $this->Html->script('add');
?>
有没有更好的方法来做到这一点,而不是将它们作为参数传递?
发布于 2013-03-02 06:06:04
你目前的做法一点也没有错。任何性能上的提升都是可以忽略不计的,而且我不认为有更优雅的方式可以做到这一点。
https://stackoverflow.com/questions/15164458
复制相似问题