Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #933: send_ajax.diff

File send_ajax.diff, 2.5 kB (added by mortonda@dgrmm.net, 3 years ago)

apply to: actionpack/lib/action_view/helpers/javascript_helper.rb

  • javascript_helper.rb

    old new  
    152152      def escape_javascript(javascript) 
    153153        (javascript || '').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" } 
    154154      end 
     155       
     156      # helper function to send a javascript command to the client. 
     157      # options hash should have: 
     158      # :action => (:insert, :replace, :delete, :empty) 
     159      # :target => DOM element. (string name) 
     160      # :position => (:before, :top, :bottom, :after)  
     161      #   postion is only used in the insert routines 
     162      # pass in a block of captured erb html for functions that make sense to do so. 
     163      def send_ajax(options, &block) 
     164         
     165        if block then 
     166          # execute the block=   
     167          # this code stolen from the capture helper 
     168          buffer = eval("_erbout", block.binding) 
     169          pos = buffer.length 
     170          block.call 
     171           
     172          # extract the block  
     173          data = buffer[pos..-1] 
     174           
     175          # replace it in the original with empty string 
     176          buffer[pos..-1] = '' 
     177          text = escape_javascript(data) 
     178        end 
     179           
     180        # Some discussion has been taking place on  
     181        # http://wiki.rubyonrails.com/rails/show/Ajax%20Discussion 
     182        # as to whether this is the fastest way to manipulate the DOM 
     183        # At the moment, it appears the safest and most expedient 
     184        case options[:action] 
     185          when :insert 
     186            case options[:position] 
     187              when :before 
     188                "new Insertion.Before('#{options[:target]}', '#{text}')" 
     189              when :top 
     190                "new Insertion.Top('#{options[:target]}', '#{text}')" 
     191              when :bottom 
     192                "new Insertion.Bottom('#{options[:target]}', '#{text}')" 
     193              when :after 
     194                "new Insertion.After('#{options[:target]}', '#{text}')" 
     195              else 
     196                raise ArgumentError, "Invalid position, choose one of :before, :top, :bottom, :after" 
     197            end 
     198          when :replace 
     199            "$('#{target}').innerHTML = '#{text}'"          
     200          when :empty 
     201            "$('#{target}').innerHTML = ''" 
     202          when :delete 
     203            "$('#{target}').parentNode.removeChild($('#{options[:target]}'))" 
     204          else 
     205            raise ArgumentError, "Invalid action, choos one of :insert, :replace, :delete, :empty" 
     206        end 
     207       
     208      end 
    155209 
    156210    private 
    157211      def options_for_ajax(options)