The shared bits

My First Rails Plugin – JavaScriptFunction

I’m working on a Rails app using RJS and Prototype Window Class. I needed to be able to return an anonymous JavaScript function in my RJS template but when passing it in a string to

page.call

it kept getting quoted and didn’t work right (as you would expect).

I solved it by creating a JavaScriptFunction class and helpers so that it’s easily usable in an RJS template. Browse the plugin or install it:

<pre>
# With subversion
./script/plugin install -x http://svn.vanderbrew.com/svn/repos/plugins/javascript_function/

# Without subversion
./script/plugin install http://svn.vanderbrew.com/svn/repos/plugins/javascript_function/
</pre>

To use it, just do:

<pre>
page.call 'Dialog.confirm', "New Thing - do you want to add it?",
            { :windowParameters => { :width => 300 },
              :sokLabel => 'Yes',
              :cancelLabel => 'No',
              :buttonClass => 'myButtonClass',
              :id => 'myDialogId',
              :cancel => anonymous_javascript_function(:parameters => ["win"],
                         :body => "$('#{@element_id}').innerHTML = '#{escape_javascript @thing.name}'"),
              :sok => anonymous_javascript_function(:parameters => ["win"],
                         :body => "return true;")
            }
</pre>

It’s my first plugin so I’m open to any feedback you have about it. Please use the Contact form to reach me.

Note – the keys

:sokLabel

and

:sok

shouldn’t have an ‘s’ in the front for real implementation with the JS library – I did that to prevent an emoticon from appearing thanks to WordPress.

Categories