Module: JQueryOnRails::Helpers::JQueryHelper::JavaScriptGenerator::GeneratorMethods

Defined in:
lib/jquery_on_rails/helpers/jquery_helper.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object (private)



209
210
211
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 209

def method_missing(method, *arguments)
  JavaScriptProxy.new(self, method.to_s.camelize)
end

Instance Method Details

#<<(javascript) ⇒ Object



155
156
157
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 155

def <<(javascript)
  @lines << javascript
end

#[](id) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 85

def [](id)
  case id
    when String, Symbol, NilClass
      JavaScriptElementProxy.new(self, id)
    else
      JavaScriptElementProxy.new(self, ActionController::RecordIdentifier.dom_id(id))
  end
end

#alert(message) ⇒ Object



134
135
136
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 134

def alert(message)
  call 'alert', message
end

#assign(variable, value) ⇒ Object



151
152
153
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 151

def assign(variable, value)
  record "#{variable} = #{javascript_object_for(value)}"
end

#call(function, *arguments, &block) ⇒ Object



147
148
149
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 147

def call(function, *arguments, &block)
  record "#{function}(#{arguments_for_call(arguments, block)})"
end

#delay(seconds = 1) ⇒ Object



159
160
161
162
163
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 159

def delay(seconds = 1)
  record "setTimeout(function() {\n\n"
  yield
  record "}, #{(seconds * 1000).to_i})"
end

#hide(*ids) ⇒ Object



126
127
128
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 126

def hide(*ids)
  loop_on_multiple_ids 'hide', ids
end

#insert_html(position, id, *options_for_render) ⇒ Object



102
103
104
105
106
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 102

def insert_html(position, id, *options_for_render)
  content = javascript_object_for(render(*options_for_render))
 position = INSERTION_POSITIONS[position.to_sym] || position.to_s.downcase
  record "jQuery('##{id}').#{position}(#{content});"
end

#literal(code) ⇒ Object



94
95
96
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 94

def literal(code)
  ::ActiveSupport::JSON::Variable.new(code.to_s)
end

#redirect_to(location) ⇒ Object



138
139
140
141
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 138

def redirect_to(location)
  url = location.is_a?(String) ? location : @context.url_for(location)
  record "window.location.href = #{url.inspect}"
end

#reloadObject



143
144
145
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 143

def reload
  record 'window.location.reload()'
end

#remove(*ids) ⇒ Object



118
119
120
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 118

def remove(*ids)
  loop_on_multiple_ids 'remove', ids
end

#replace(id, *options_for_render) ⇒ Object



113
114
115
116
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 113

def replace(id, *options_for_render)
  content = javascript_object_for(render(*options_for_render))
  record "jQuery('##{id}').replaceWith(#{content});"
end

#replace_html(id, *options_for_render) ⇒ Object



108
109
110
111
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 108

def replace_html(id, *options_for_render)
  content = javascript_object_for(render(*options_for_render))
  record "jQuery('##{id}').html(#{content});"
end

#select(pattern) ⇒ Object



98
99
100
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 98

def select(pattern)
  JavaScriptElementCollectionProxy.new(self, pattern)
end

#show(*ids) ⇒ Object



122
123
124
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 122

def show(*ids)
  loop_on_multiple_ids 'show', ids
end

#to_sObject

:nodoc:



75
76
77
78
79
80
81
82
83
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 75

def to_s #:nodoc:
  returning javascript = @lines * $/ do
    if ActionView::Base.debug_rjs
      source = javascript.dup
      javascript.replace "try {\n#{source}\n} catch (e) "
      javascript << "{ alert('RJS error:\\n\\n' + e.toString()); alert('#{source.gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }}'); throw e }"
    end
  end
end

#toggle(*ids) ⇒ Object



130
131
132
# File 'lib/jquery_on_rails/helpers/jquery_helper.rb', line 130

def toggle(*ids)
  loop_on_multiple_ids 'toggle', ids
end