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

Ticket #933: send_ajax.3.diff

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

added formfill action which can fill in all the fields for a given AR object.

  • 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, :formfill) 
     159      # :target => DOM element. (string name) 
     160      # :position => (:before, :top, :bottom, :after)  
     161      #   postion is only used in the insert routines 
     162      # :formobject => ActiveRecord Object - values to fill into form 
     163      #   :formobject is used only by :formfill action 
     164      # pass in a block of captured erb html for functions that make sense to do so. 
     165      def send_ajax(options, &block) 
     166         
     167        if block then 
     168          # execute the block=   
     169          # this code stolen from the capture helper 
     170          buffer = eval("_erbout", block.binding) 
     171          pos = buffer.length 
     172          block.call 
     173           
     174          # extract the block  
     175          data = buffer[pos..-1] 
     176           
     177          # replace it in the original with empty string 
     178          buffer[pos..-1] = '' 
     179          text = escape_javascript(data) 
     180        end 
     181           
     182        case options[:action] 
     183          when :insert 
     184            case options[:position] 
     185              when :before 
     186                "new Insertion.Before('#{options[:target]}', '#{text}')" 
     187              when :top 
     188                "new Insertion.Top('#{options[:target]}', '#{text}')" 
     189              when :bottom 
     190                "new Insertion.Bottom('#{options[:target]}', '#{text}')" 
     191              when :after 
     192                "new Insertion.After('#{options[:target]}', '#{text}')" 
     193              else 
     194                raise ArgumentError, "Invalid position, choose one of :before, :top, :bottom, :after" 
     195            end 
     196          when :replace 
     197            "$('#{target}').innerHTML = '#{text}'"          
     198          when :empty 
     199            "$('#{target}').innerHTML = ''" 
     200          when :delete 
     201            "$('#{target}').parentNode.removeChild($('#{options[:target]}'))" 
     202          when :formfill 
     203            raise ArgumentError, ":formfill requires :formobject" unless options[:formobject] 
     204            ret = '' 
     205            objectname = options[:formobject].class.to_s.underscore 
     206            for key,value in options[:formobject].attributes 
     207              ret << "$('#{objectname}_#{key}').value = '#{escape_javascript(value.to_s)}'\n" 
     208            end 
     209            return ret 
     210          else 
     211            raise ArgumentError, "Invalid action, choose one of :insert, :replace, :delete, :empty, :formfill" 
     212        end 
     213       
     214      end 
    155215 
    156216    private 
    157217      def options_for_ajax(options)