diff options
author | Yehuda Katz <wycats@gmail.com> | 2009-07-20 16:13:21 -0700 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-07-20 16:13:21 -0700 |
commit | d80316ad3269e11c2be9be0727ae518f5e427d24 (patch) | |
tree | 62274354da94c0d008ab8fc3192c44af3262d040 /actionpack/lib/action_view | |
parent | 37658f15bb88e054635a496327a4a82bb50fd5d5 (diff) | |
download | rails-d80316ad3269e11c2be9be0727ae518f5e427d24.tar.gz rails-d80316ad3269e11c2be9be0727ae518f5e427d24.tar.bz2 rails-d80316ad3269e11c2be9be0727ae518f5e427d24.zip |
First effort at new Ajax helpers
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/helpers.rb | 1 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/ajax_helper.rb | 68 |
2 files changed, 69 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers.rb b/actionpack/lib/action_view/helpers.rb index c1c0eb59ae..652561f7f8 100644 --- a/actionpack/lib/action_view/helpers.rb +++ b/actionpack/lib/action_view/helpers.rb @@ -1,6 +1,7 @@ module ActionView #:nodoc: module Helpers #:nodoc: autoload :ActiveModelHelper, 'action_view/helpers/active_model_helper' + autoload :AjaxHelper, 'action_view/helpers/ajax_helper' autoload :AssetTagHelper, 'action_view/helpers/asset_tag_helper' autoload :AtomFeedHelper, 'action_view/helpers/atom_feed_helper' autoload :BenchmarkHelper, 'action_view/helpers/benchmark_helper' diff --git a/actionpack/lib/action_view/helpers/ajax_helper.rb b/actionpack/lib/action_view/helpers/ajax_helper.rb new file mode 100644 index 0000000000..9cc2acc239 --- /dev/null +++ b/actionpack/lib/action_view/helpers/ajax_helper.rb @@ -0,0 +1,68 @@ +module ActionView + module Helpers + module AjaxHelper + include UrlHelper + + def link_to_remote(name, url, options = {}) + html = options.delete(:html) || {} + + update = options.delete(:update) + if update.is_a?(Hash) + html["data-update-success"] = update[:success] + html["data-update-failure"] = update[:failure] + else + html["data-update-success"] = update + end + + html["data-update-position"] = options.delete(:position) + html["data-method"] = options.delete(:method) + html["data-remote"] = "true" + + html.merge!(options) + + url = url_for(url) if url.is_a?(Hash) + link_to(name, url, html) + end + + def button_to_remote(name, options = {}, html_options = {}) + url = options.delete(:url) + url = url_for(url) if url.is_a?(Hash) + + html_options.merge!(:type => "button", :value => name, + :"data-url" => url) + + tag(:input, html_options) + end + + module Rails2Compatibility + def set_callbacks(options, html) + [:complete, :failure, :success, :interactive, :loaded, :loading].each do |type| + html["data-#{type}-code"] = options.delete(type.to_sym) + end + + options.each do |option, value| + if option.is_a?(Integer) + html["data-#{option}-code"] = options.delete(option) + end + end + end + + def link_to_remote(name, url, options = nil) + if !options && url.is_a?(Hash) && url.key?(:url) + url, options = url.delete(:url), url + end + + set_callbacks(options, options[:html] ||= {}) + + super + end + + def button_to_remote(name, options = {}, html_options = {}) + set_callbacks(options, html_options) + super + end + end + + end + end +end
\ No newline at end of file |