Zend Framework: How to generate javascript only once? -


i creating custom zend form element require javascript. can add javascript need this:

/**  * renders javascript view.  */ protected function _generatejavascript() {     $this->view->headscript()->capturestart(); ?>          $(document).ready(function(){             alert('hello');         });      <?php $this->view->headscript()->captureend(); } 

however, if custom form element used multiple times, identical javascript appear multiple times. there way render javascript once?

you might able like

protected function _generatejavascript() {     if (!isset($this->view->scriptgenerated))     {         // generate script, then...         $this->view->scriptgenerated = true;     } } 

since php objects can have "extra" properties.

you'll want pick better name, won't clash if have multiple scripts using same pattern.

alternatively, have script in file. looks appendfile won't include script file more once. don't see such protection ad-hoc scripts.

protected function _generatejavascript() {     $this->view->headscript()->appendfile('path/to/script.js'); } 

Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

c++ - Convert big endian to little endian when reading from a binary file -