Class: Aurita::GUI::Javascript

Inherits:
Object
  • Object
show all
Defined in:
lib/aurita-gui/html.rb

Overview

A minimalistic generator for javascript calls. Usage:

my_element.onclick = Javascript.new("alert('an alert message');")

or just:

my_element.onclick = Javascript.my_function('foo', 2, 'bar')
--> my_function('foo', 2, 'bar');

For namespaces:

my_element.onclick = Javascript[:Foo].my_function(1,2)
--> Foo.my_function('foo', 2, 'bar');

Method #string renders the code passed in constructir, enclosed in <script> tags:

Javascript.new(“alert(‘message’);”).string

--> 
<script language="Javascript" type="text/javascript">
alert('message'); 
</script>

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script) ⇒ Javascript

Returns a new instance of Javascript.



289
290
291
# File 'lib/aurita-gui/html.rb', line 289

def initialize(script)
  @script = script
end

Class Method Details

.[](namespace) ⇒ Object



305
306
307
# File 'lib/aurita-gui/html.rb', line 305

def self.[](namespace) 
  return self.new(namespace.to_s + '.')
end

.method_missing(meth, *args) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/aurita-gui/html.rb', line 309

def self.method_missing(meth, *args)
  args = args.collect { |arg|
    if arg.instance_of? String then
      '\'' << arg << '\''
    else
      arg
    end
  }
  args_string = args.join(',')
  meth.to_s << '(' << args_string + ');'
end

Instance Method Details

#code_blockObject

Renders script to a <script> block.



294
295
296
297
298
# File 'lib/aurita-gui/html.rb', line 294

def code_block
  '<script language="Javascript" type="text/javascript">' << "\n" << 
    @script + "\n" << 
  '</script>'
end

#stringObject Also known as: to_s



300
301
302
# File 'lib/aurita-gui/html.rb', line 300

def string
  @script
end