Zend Framework: seek to MVC Javascript, CSS

With the gradual learning Zend Framework and build it using basic applications I noticed that the client js code and an inline styles into the view scripts, as they occupy nearly half of the entire script. In principle, nothing wrong with that, but to me this trash began to put pressure on the eye. In addition, an inline js is gradually becoming tied to the view script design, totally dependent both from the script and transmitted by the data controller. With all this leaves no desire to write javascript code as nice as possible with the help of the jQuery library.

So that in this case it is possible to do. His first thought was to make an inline scripts and styles in external files and connect them via view helpers headLink and headStyle. If the styles are all generally clear — a very specific style for a specific type to make a css file and attach it as necessary, then js remains data-dependent.

As the first solution to write a plugin for frontController to connect as required for the needed files:

    class My_Controller_Plugin_Webinit extends Zend_Controller_Plugin_Abstract

    {

    public function preDispatch()

    { the

  1. $controllerName = $this->_request->getControllerName();
  2. the
  3. $actionName = $this->_request->getActionName();
  4. the

  5. $view = Zend_Layout::getMvcInstance()->getView();
  6. if ( file_exists(APPLICATION_CSS_FOLDER.'/'. the

  7. $controllerName.'/'.
  8. the
  9. $actionName.'.css') )
  10. { the

  11. $view->assign('cssControllerAction'$controllerName.'/'.$actionName.'.css');
  12. the
  13. }
  14. if ( file_exists(APPLICATION_JS_FOLDER.'/'. the

  15. $controllerName.'/'.
  16. the
  17. $actionName.'.js') )
  18. { the

  19. $view->assign('jsControllerAction' $controllerName.'/'.$actionName.'.js');
  20. the
  21. }
  22. the
  23. }
  24. the
  25. }
* This source code was highlighted with Source Code Highlighter.


Don't forget to Bootstrap the class connect created a plugin:

    protected function _initFrontControllerPlugins()

    { the

  1. $frontController = Zend_Controller_Front::getInstance();
  2. the
  3. $frontController- > registerPlugin( new My_Controller_Plugin_Webinit() );
  4. the
  5. }
* This source code was highlighted with Source Code Highlighter.


As is clear, used css and js files are placed under full analogy to embed scripts. Only instead of /application/views/script/controllerName/actionName/ root directory for them to be respectively public/css/ and public/js/. For this you need to declare three constants in index.php:

    the
  1. defined('APPLICATION_PUBLIC_FOLDER')
  2. the
  3. || define( 'APPLICATION_PUBLIC_FOLDER'
  4. the
  5. dirname(__FILE__) );
  6. the

  7. defined('APPLICATION_CSS_FOLDER')
  8. the
  9. || define( 'APPLICATION_CSS_FOLDER'
  10. the
  11. realpath(APPLICATION_PUBLIC_FOLDER . '/css') );
  12. the

  13. defined('APPLICATION_JS_FOLDER')
  14. the
  15. || define( 'APPLICATION_JS_FOLDER'
  16. the
  17. realpath(APPLICATION_PUBLIC_FOLDER . '/js') );
* This source code was highlighted with Source Code Highlighter.


The plugin allows you to connect to our Layout necessary files with the following code:

    <? if ($this->cssControllerAction) $this->headLink()->appendStylesheet('/css/'.$this->cssControllerAction)?>

    <? if ($this->jsControllerAction) $this->headScript()->appendFile('/js/'.$this->jsControllerAction)?> Open the browser and the required files joined.

    The question remains with javascripta. We placed the code into an external file within the public directory. But there is a need, for example, display a welcome text on the basis of the dictionary function alert(). Of course, for translation use Zend_Translate, but then again, what if all our data is available only in the view script?

    For your own needs, I found the perfect solution to the organization of namespaces. For example, to the interpreter inside a view script, we need a limited set of phrases. So we can arrange the storage of these phrases and give them the required handler-level js.

    This is how it happens.

    In the main js file of my project application.js is the setting global methods, objects, and handlers. Here you can add:

      the
    1. $(document).ready(function(){
    2. the
    3. $.TRANSLATION = {};
    4. the
    5. });
    * This source code was highlighted with Source Code Highlighter.


    Store oganesovna, you now need to fill it.

    To do this in the view script we need the following code:

      <script type="text/javascript"> the

    1. $(document).ready(function(){
    2. the
    3. $.data($.TRANSLATION "Nice to see you!" '<?=$this->translate("Nice to see you!")?>');
    4. </script>

    * This source code was highlighted with Source Code Highlighter.


    To use the data from the repository we can use any function handling this phase of the project, through to store and retrieve the data:

      alert( $.data($.TRANSLATION "Nice to see you!") );

    * This source code was highlighted with Source Code Highlighter.


    So, we get the following picture: for controller index, action index there is a file in the public directory /js/index/index.js that is a set of functions to work. All necessary data is written to the data store directly in the view script, and the vault itself is declared globally for all view scripts in the main js file of the application.

    In the MVC model. A bit far-fetched, but still.

    As the controller (response to user) is a plugin for frontController which is organizing the connection, the script of the layout and base — application.js.

    As a model is the view script, accumulating data for the js repository.

    As supports plug-in javascript code, fully responsible for client view.

    In the vault, you can accumulate any data for future use.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Integration of PostgreSQL with MS SQL Server for those who want faster and deeper

Custom database queries in MODx Revolution

Parse URL in Zend Framework 2