Class: Aurita::GUI::Javascript
- Inherits:
-
Object
- Object
- Aurita::GUI::Javascript
- Defined in:
- lib/aurita-gui/javascript.rb
Overview
A minimalistic generator for javascript calls.
Javascript is a simple string factory so you can write pieces of javascript code in ruby instead of using plain old strings. This does not introduce functionality, it just eases the rendering of ruby objects to javascript: Aurita::GUI::Javascript renders method parameters to their respective meaning in javascript, and also understands namespaces, variables, and object notation.
Sometimes i needed somethind like this in ERB templates:
<%= button(:class => :cancel, :onclick => "Aurita.cancel_form('#{form.dom_id}"')) %>
Now i can do the same in pure ruby:
<%= button(:class => :cancel, :onclick => js.Aurita.cancel_form(form.dom_id)) %>
Just by defining a helper method in 5 lines of code (see below, section Template helper example).
Usage with block:
Javascript.build { |j|
j.Some.Namespace.do_stuff()
j.bla(123, { :gna => 23, :blu => '42' })
j.alert("escape 'this' string")
}
-->
Some.Namespace.do_stuff(); bla(123,{gna: 23, blu: '42' }); alert('escape \'this\' string');
Calling class methods
For a single call of a javascipt method, you can also use:
my_element.onclick = Javascript.alert('an alert message')
--> alert('an alert message');
This also provides method chaining. Uppercase methods are interpreted as namespaces.
my_element.onclick = Javascript.Some.Namespace.my_function('foo', 2, 'bar')
--> <div onclick="Some_Namespace.my_function('foo', 2, 'bar'); "> ... </div>
Template helper example
In Aurita, there’s a simple helper, defined as
def js(&block)
if block_given? then
Javascript.build(&block)
else
Javascript
end
end
It works like that:
input = Input_Field.new(:onchange => js.Aurita.notify_change_of(:this))
--> <input onchange="Aurita.notify_change_of(this); " />
or
input.onfocus = js.Aurita.notify_focus_on(:this)
--> <input onchange="Aurita.notify_change_of(this); "
onfocus="Aurita.notify_focus_of(this); " />
Render to <script> tag
Method #to_tag renders the code enclosed in <script> tags:
Javascript.new(“alert(‘message’);”).to_tag
-->
<script language="Javascript" type="text/javascript">
alert('message');
</script>
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(script = '') ⇒ Javascript
constructor
A new instance of Javascript.
- #method_missing(meth, *args) ⇒ Object
- #render ⇒ Object
- #string ⇒ Object (also: #to_s)
-
#to_tag ⇒ Object
Renders script to a <script> block.
Constructor Details
#initialize(script = '') ⇒ Javascript
Returns a new instance of Javascript.
86 87 88 89 |
# File 'lib/aurita-gui/javascript.rb', line 86 def initialize(script='') @script = script @scripy ||= '' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/aurita-gui/javascript.rb', line 115 def method_missing(meth, *args) args = args.collect { |arg| arg = to_js(arg) } args_string = args.join(',') if args.size > 0 meth = meth.to_s # Uppercase method => method call meth << '(' << args_string.to_s << '); ' if meth[0].to_i > 96 # Uppercase method => Namespace meth << '.' if meth[0].to_i < 97 @script << meth return self end |
Class Method Details
.[](namespace) ⇒ Object
111 112 113 |
# File 'lib/aurita-gui/javascript.rb', line 111 def self.[](namespace) return self.new(namespace.to_s + '.') end |
.build(&block) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/aurita-gui/javascript.rb', line 91 def self.build(&block) if block.arity > 0 then yield(Javascript.new) else self.class_eval(&block) end end |
.method_missing(meth, *args) ⇒ Object
129 130 131 |
# File 'lib/aurita-gui/javascript.rb', line 129 def self.method_missing(meth, *args) return self.new().__send__(meth, *args) end |
Instance Method Details
#render ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/aurita-gui/javascript.rb', line 128 def method_missing(meth, *args) args = args.collect { |arg| arg = to_js(arg) } args_string = args.join(',') if args.size > 0 meth = meth.to_s # Uppercase method => method call meth << '(' << args_string.to_s << '); ' if meth[0].to_i > 96 # Uppercase method => Namespace meth << '.' if meth[0].to_i < 97 @script << meth return self end |
#string ⇒ Object Also known as: to_s
106 107 108 |
# File 'lib/aurita-gui/javascript.rb', line 106 def string @script end |
#to_tag ⇒ Object
Renders script to a <script> block.
100 101 102 103 104 |
# File 'lib/aurita-gui/javascript.rb', line 100 def to_tag '<script language="Javascript" type="text/javascript">' << "\n" << @script + "\n" << '</script>' end |