racovi

racovi is a gem to register initialization code for your javascript which is executed only for the current controller and view.

I created that to easily deploy what is explain in this blog post.

racovi is based on the javascript event stack Hevents.

** Important Note ** : This is a early version of the gem and still need some manual installations in your Rails application. But javascript code works

Usage

Prepare your Rails layout

Put the helper function init_view_js wherever you want in your layouts, it will add a $(document).ready(…) section calling initializer code for the current controller and the current view.

for example

    /** application.html.haml **/
    …
    = init_view_js
    …

Prepare your application.js

add these line to your app/assets/javascript/application.js

    //= require hevents
    //= require racovi

Register an initializer code

use register_init(names,fun) in your controller or view js files

    /** assets/javascript/persons.js **/
    …
    Racovi.register_init('persons', function(){
      console.log('hello from Person controller')
    });
    …

_

    /** assets/javascript/persons.js.coffee **/
    …
    Racovi.register_init 'persons', () ->
      console.log('hello from Person controller')
    …

register initializer for multiple views

    /** assets/javascript/persons.js.coffee **/
    ...
    Racovi.register_init ['persons_new','persons_edit'], () ->
      console.log('editing a Person')
    …

cumulative initializer for complex views

If you have complex view GUI distributed on multiple sources files, independent from each others, you can register multiple time the same initializer name, all registered functions will be called.

    /** assets/javascript/sidebar.js.coffee **/
    …
    Racovi.register_init 'persons', () ->
            ('#persons_menu').show()
    …
    /** assets/javascript/persons.js.coffee **/
    ...
    Racovi.register_init 'persons', () ->
      console.log('hello from Person controller')
    ...

TODO

Contributing to racovi

Copyright

Copyright © 2012 yann ARMAND under MIT See LICENSE.txt for further details.