Skip to content
This repository
Browse code

Changed data-name to data-observed on observe_field

  • Loading branch information...
commit 8e172f13d7f43b999b7028153ac6327ff592c72a 1 parent 261654b
Bob Remeika authored November 04, 2009 stefanpenner committed January 27, 2010
30  actionpack/lib/action_view/helpers/ajax_helper.rb
@@ -55,20 +55,12 @@ def periodically_call_remote(options = {})
55 55
       end
56 56
 
57 57
       def observe_field(name, options = {})
58  
-        attributes = extract_remote_attributes!(options)
59  
-        callback = options.delete(:function)
60  
-        frequency = options.delete(:frequency)
  58
+        options[:observed] = name
61 59
 
62  
-        attributes["data-name"] = name
  60
+        attributes = extract_remote_attributes!(options)
  61
+        attributes.merge!(extract_observer_attributes!(options))
63 62
         attributes["data-js-type"] = "field_observer"
64 63
 
65  
-        if callback
66  
-          attributes["data-observer-code"] = create_js_function(callback, "element", "value")
67  
-        end
68  
-        if frequency && frequency != 0
69  
-          attributes["data-frequency"] = frequency.to_i
70  
-        end
71  
-
72 64
         script_decorator(attributes)
73 65
       end
74 66
 
@@ -146,6 +138,22 @@ def extract_update_attributes!(options)
146 138
         purge_unused_attributes!(attributes)
147 139
       end
148 140
 
  141
+      def extract_observer_attributes!(options)
  142
+        attributes = {}
  143
+        attributes["data-observed"] = options.delete(:observed)
  144
+
  145
+        callback = options.delete(:function)
  146
+        frequency = options.delete(:frequency)
  147
+        if callback
  148
+          attributes["data-observer-code"] = create_js_function(callback, "element", "value")
  149
+        end
  150
+        if frequency && frequency != 0
  151
+          attributes["data-frequency"] = frequency.to_i
  152
+        end
  153
+
  154
+        attributes
  155
+      end
  156
+
149 157
       def purge_unused_attributes!(attributes)
150 158
         attributes.delete_if {|key, value| value.nil? }
151 159
         attributes
14  actionpack/test/javascript/ajax_test.rb
@@ -382,17 +382,17 @@ def field(options = {})
382 382
 
383 383
   test "using a url string" do
384 384
     assert_html field(:url => "/some/other/url"),
385  
-      %w(script data-url="/some/other/url" data-name="title")
  385
+      %w(script data-url="/some/other/url" data-observed="title")
386 386
   end
387 387
 
388 388
   test "using a url hash" do
389 389
     assert_html field(:url => {:controller => :blog, :action => :update}),
390  
-      %w(script data-url="/url/hash" data-name="title")
  390
+      %w(script data-url="/url/hash" data-observed="title")
391 391
   end
392 392
 
393 393
   test "using a :frequency option" do
394 394
     assert_html field(:url => { :controller => :blog }, :frequency => 5.minutes),
395  
-      %w(script data-url="/url/hash" data-name="title" data-frequency="300")
  395
+      %w(script data-url="/url/hash" data-observed="title" data-frequency="300")
396 396
   end
397 397
 
398 398
   test "using a :frequency option of 0" do
@@ -401,21 +401,21 @@ def field(options = {})
401 401
 
402 402
   test "observe field with common options" do
403 403
     assert_html observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }),
404  
-      %w(script data-name="glass" data-frequency="300" data-url="/url/hash")
  404
+      %w(script data-observed="glass" data-frequency="300" data-url="/url/hash")
405 405
   end
406 406
 
407 407
   # TODO: Consider using JSON instead of strings.  Is using 'value' as a magical reference to the value of the observed field weird? (Rails2 does this) - BR
408 408
   test "using a :with option" do
409 409
     assert_html field(:with => "foo"),
410  
-      %w(script data-name="title" data-with="'foo=' + encodeURIComponent(value)")
  410
+      %w(script data-observed="title" data-with="'foo=' + encodeURIComponent(value)")
411 411
 
412 412
     assert_html field(:with => "'foo=' + encodeURIComponent(value)"),
413  
-      %w(script data-name="title" data-with="'foo=' + encodeURIComponent(value)")
  413
+      %w(script data-observed="title" data-with="'foo=' + encodeURIComponent(value)")
414 414
   end
415 415
 
416 416
   test "using json in a :with option" do
417 417
     assert_html field(:with => "{'id':value}"),
418  
-      %w(script data-name="title" data-with="{'id':value}")
  418
+      %w(script data-observed="title" data-with="{'id':value}")
419 419
   end
420 420
 
421 421
   test "using :function for callback" do

0 notes on commit 8e172f1

Please sign in to comment.
Something went wrong with that request. Please try again.