174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/nested.rb', line 174
def angular_add_functions(js, resource)
resource.actions.each do |e|
method, action = e.values_at :method, :action
fun_name = Nested::JsUtil::generate_function_name(resource, method, action)
args = Nested::JsUtil::function_arguments(resource)
route_args = args.inject({}) do |memo, e|
idx = args.index(e)
memo[:"#{e}_id"] = "'+(typeof(values[#{idx}]) == 'number' ? values[#{idx}].toString() : values[#{idx}].id)+'"
memo
end
route = "#{self.prefix}" + resource.route(route_args)
js << " impl.#{fun_name} = function(#{args.join(',')}){"
args = args.map{|a| "$q.when(#{a})"}
js << " return $q.all([#{args.join(',')}]).then(function(values){" unless args.empty?
js << (args.length > 1 ? " " : "") + " return $http({method: '#{method}', url: '#{route}'})"
js << " });" unless args.empty?
js << " }"
end
resource.resources.each {|r| angular_add_functions(js, r) }
end
|