maiha-mjs 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/mjs/helper.rb +34 -18
- metadata +1 -1
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ AUTHOR = "maiha"
|
|
9
9
|
EMAIL = "maiha@wota.jp"
|
10
10
|
HOMEPAGE = "http://github.com/maiha/mjs"
|
11
11
|
SUMMARY = "A slice for the Merb framework that offers Ajax actions like RJS with jQuery"
|
12
|
-
GEM_VERSION = "0.0.
|
12
|
+
GEM_VERSION = "0.0.5"
|
13
13
|
|
14
14
|
spec = Gem::Specification.new do |s|
|
15
15
|
s.rubyforge_project = 'merb'
|
data/lib/mjs/helper.rb
CHANGED
@@ -8,11 +8,14 @@ module Mjs
|
|
8
8
|
|
9
9
|
def remote_function(opts)
|
10
10
|
build_href(opts)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
"$.getScript('#{opts[:href]}')"
|
11
|
+
unless opts[:submit]
|
12
|
+
opts[:url] ||= opts[:href]
|
13
|
+
opts[:dataType] = "script"
|
15
14
|
end
|
15
|
+
function = "jQuery.ajax(%s);" % options_for_ajax(opts)
|
16
|
+
confirm = opts.delete(:confirm)
|
17
|
+
function = "if (confirm('#{escape_javascript(confirm)}')) { #{function}; }" if confirm
|
18
|
+
return function
|
16
19
|
end
|
17
20
|
|
18
21
|
# experimental: not tested yet
|
@@ -84,26 +87,32 @@ module Mjs
|
|
84
87
|
:xhr => :xhr,
|
85
88
|
}
|
86
89
|
|
90
|
+
|
87
91
|
def options_for_ajax(options)
|
88
92
|
js_options = build_callbacks!(options)
|
89
93
|
|
90
94
|
submit = options.delete(:submit)
|
91
95
|
target =
|
92
96
|
case submit
|
93
|
-
when Symbol then "
|
94
|
-
when String then "
|
97
|
+
when Symbol then "jQuery('##{submit} input, ##{submit} textarea')"
|
98
|
+
when String then "jQuery('#{submit}')"
|
95
99
|
when NilClass # GET requst
|
96
100
|
else
|
97
101
|
raise ArgumentError, "link_to :submit expects Symbol or String, but got #{submit.class.name}"
|
98
102
|
end
|
99
|
-
|
100
103
|
build_href(options)
|
101
104
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
+
if target
|
106
|
+
js_options[:type] = "'POST'"
|
107
|
+
js_options[:data] = "#{target}.serialize()"
|
108
|
+
end
|
109
|
+
js_options[:url] = "'#{options[:url] || options[:href]}'"
|
105
110
|
js_options[:dataType] = "'script'"
|
106
111
|
|
112
|
+
if js_options[:url].blank?
|
113
|
+
raise "Cannot build ajax options because url is blank. (#{options.inspect})"
|
114
|
+
end
|
115
|
+
|
107
116
|
options_for_javascript(js_options)
|
108
117
|
end
|
109
118
|
|
@@ -117,8 +126,8 @@ module Mjs
|
|
117
126
|
|
118
127
|
def jquery_selector(key)
|
119
128
|
case key
|
120
|
-
when Symbol then "
|
121
|
-
when String then "
|
129
|
+
when Symbol then "jQuery('##{key}')"
|
130
|
+
when String then "jQuery('#{key}')"
|
122
131
|
else
|
123
132
|
raise "invalid jquery selector: [#{key.class}] #{key}"
|
124
133
|
end
|
@@ -128,11 +137,19 @@ module Mjs
|
|
128
137
|
def build_callbacks!(options)
|
129
138
|
callbacks = {}
|
130
139
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
140
|
+
[:before, :complete].each do |event|
|
141
|
+
options[event] = Array(options[event])
|
142
|
+
end
|
143
|
+
|
144
|
+
# special callback (:spinner)
|
145
|
+
if options[:spinner]
|
146
|
+
target = jquery_selector(options.delete(:spinner))
|
147
|
+
options[:before] << "#{target}.show()"
|
148
|
+
options[:complete] << "#{target}.hide()"
|
149
|
+
end
|
150
|
+
|
151
|
+
[:before, :complete].each do |event|
|
152
|
+
options[event] = options[event].compact * ';'
|
136
153
|
end
|
137
154
|
|
138
155
|
options.each do |callback, code|
|
@@ -144,6 +161,5 @@ module Mjs
|
|
144
161
|
|
145
162
|
callbacks
|
146
163
|
end
|
147
|
-
|
148
164
|
end
|
149
165
|
end
|