| Class | Mack::JavaScript::Framework::JqueryAjax |
| In: |
lib/mack-javascript/helpers/jquery_helper.rb
|
| Parent: | Object |
# File lib/mack-javascript/helpers/jquery_helper.rb, line 9
9: def remote_function(options)
10: javascript_options = options_for_ajax(options)
11: function = "$.ajax(#{javascript_options})"
12:
13: function = "#{options[:before]}; #{function}" if options[:before]
14: function = "#{function}; #{options[:after]}" if options[:after]
15: function = "if (#{options[:condition]}) { #{function}; }" if options[:condition]
16: function = "if (confirm('#{escape_javascript(options[:confirm])}')) { #{function}; }" if options[:confirm]
17: return function
18: end
# File lib/mack-javascript/helpers/jquery_helper.rb, line 53
53: def append_ajax_data(data, new_data)
54: if data
55: data << " + '&"
56: else
57: data = "'"
58: end
59: data << "#{new_data}'"
60: end
# File lib/mack-javascript/helpers/jquery_helper.rb, line 66
66: def build_callbacks(options)
67: callbacks = {}
68: options[:beforeSend] = '';
69: [:uninitialized,:loading,:loaded].each do |key|
70: options[:beforeSend] << (options[key].last == ';' ? options.delete(key) : options.delete(key) << ';') if options[key]
71: end
72: options.delete(:beforeSend) if options[:beforeSend].blank?
73: options[:error] = options.delete(:failure) if options[:failure]
74: if options[:update]
75: options[:success] = build_update(options) << (options[:success] ? options[:success] : '')
76: end
77: options.each do |callback, code|
78: if [:beforeSend, :complete, :error, :success ].include?(callback)
79: callbacks[callback] = "function(request){#{code}}"
80: end
81: end
82: callbacks
83: end
# File lib/mack-javascript/helpers/jquery_helper.rb, line 85
85: def build_update(options)
86: insertion = 'html'
87: insertion = options[:position].to_s.downcase if options[:position]
88: insertion = 'append' if insertion == 'bottom'
89: insertion = 'prepend' if insertion == 'top'
90: "$('##{options[:update]}').#{insertion}(request);"
91: end
# File lib/mack-javascript/helpers/jquery_helper.rb, line 21
21: def options_for_ajax(options)
22: js_options = build_callbacks(options)
23: js_options['url'] = "'#{options[:url]}'"
24: js_options['async'] = options[:type] != :synchronous
25:
26:
27: js_options['dataType'] = options[:datatype] ? "'#{options[:datatype]}'" : (options[:update] ? nil : "'script'")
28:
29:
30: if options[:form]
31: js_options['data'] = "$.param($(this).serializeArray())"
32: elsif options[:with]
33: js_options['data'] = options[:with].gsub('Form.serialize(this.form)','$.param($(this.form).serializeArray())')
34: end
35:
36: if options[:method].nil? || options[:method].to_sym != :get
37: js_options['type'] = "'post'"
38: else
39: js_options['type'] = "'get'"
40: end
41:
42: if options[:method] && options[:method].to_sym == :put || options[:method] == :delete
43: js_options['data'] = append_ajax_data(js_options['data'], "_method=#{options[:method]}")
44: end
45:
46: if js_options['type'] == "'post'" && options[:authenticity_token]
47: js_options['data'] = append_ajax_data(js_options['data'], "__authenticity_token=#{options.delete(:authenticity_token)}")
48: end
49:
50: options_for_javascript(js_options.reject {|key, value| value.nil?})
51: end