Adding page-specific Javascript to each view in CakePHP

前端 未结 9 1131
余生分开走
余生分开走 2020-12-28 09:35

In an attempt to keep my scripts maintainable, I\'m going to move each into their own file, organised by controller and action:

// scripts which only apply t         


        
相关标签:
9条回答
  • 2020-12-28 09:58

    The best way I can think of is to create your own custom AppView and have all your controllers use that:

    class myController extends AppController {
      var view = 'AppView';
      ...
    }
    

    Then, somewhere in your AppView, you'd want to do something like:

    function __construct(&$controller, $register){
      parent::__construct($controller,$register);
      $this->addScript('<script type="text/javascript" src="/path/to/js/' . $this->controller . '/' . $this->action . '.js"></script>');
    }
    

    But I'd take a step back and think about a few things, first.

    How big are your scripts, on average? Is the overhead of an external script call (before the script is cached by the client) better than adding a few hundred bytes to your main output stream (by just sticking the script into the page, inline)?

    Perhaps you'd be better of somewhere in the middle -- split your scripts up by controller, but not action. That way, after the first visit to any action, the client has all scripts for all actions. This way, you avoid a big initial download for all the application's script, but you avoid adding N http round-trips (where N is the number of actions a brand new user visits).

    Another way to approach the problem is to do it all in javascript. Just figure out a lazy-loading scheme. So your app just loads a very small loader.js, and that script figures out which other javascript sources to pull in.

    Note: I've never tested my extend-the-view hack, but I bet it'll work if you really want to do this.

    0 讨论(0)
  • 2020-12-28 09:59

    Very easy to do in your default.ctp layout file:

    An example to automatically include .css files per controller and/or controller/action (because I had this lying around, easily adaptable to .js files):

    <head>
    ...
    <?php
        if (is_file(WWW_ROOT . 'css' . DS . $this->params['controller'] . '.css')) {
            echo $html->css($this->params['controller']);
        }
        if (is_file(WWW_ROOT . 'css' . DS . $this->params['controller'] . DS . $this->params['action'] . '.css')) {
            echo $html->css($this->params['controller'] . '/' . $this->params['action']);
        }
    ?>
    ...
    </head>
    
    0 讨论(0)
  • 2020-12-28 10:01

    Kinda new to this, but after reading this added the following to my layout:

    if ($handle = opendir(WWW_ROOT . 'js' . DS . $this->params['controller'] . DS . $this->params['action'])) 
    {
        while (false !== ($entry = readdir($handle)))
            {
               $entry = str_replace(".js", "", $entry);
               echo $this->Html->script($entry);
    
        }
        closedir($handle);
    }
    
    0 讨论(0)
提交回复
热议问题