Best way to react to form changes...

1 view
Skip to first unread message

louis w

unread,
Jun 10, 2008, 11:22:48 AM6/10/08
to Ruby on Rails: Spinoffs
Wondering what is the best way to react to field changes and do
something with the field.

i was trying Form.Observer, however it looks like this only gets
passed the serialized contents of the form, not the actual element
which was changed. Am I missing something?

new Form.Observer($(form), 0.3, function(form, value){
console.log(value);
});

What would you suggest as as the best route? Whenever a field in my
form is changed I want to do some checks based on it's class, so I
need to return the element.

Should I assign a listener to each field? Should I try to get it to
work with one listener and use bubbling?

Thanks.

Frederick Polgardy

unread,
Jun 10, 2008, 12:02:02 PM6/10/08
to rubyonrail...@googlegroups.com
Bubbling seems like the answer to me.  Put any listeners you need (change, select, click, etc.) on the form element, and then in your callback, use event.element() to get the source element.  From there you can look at the new value.

-Fred


On Tue, Jun 10, 2008 at 10:22 AM, louis w <louis...@gmail.com> wrote:
Should I assign a listener to each field? Should I try to get it to
work with one listener and use bubbling?

--
Science answers questions; philosophy questions answers.

louis w

unread,
Jun 10, 2008, 1:34:07 PM6/10/08
to Ruby on Rails: Spinoffs
Are you saying I need to assign a listener to each input field?

On Jun 10, 12:02 pm, "Frederick Polgardy" <f...@polgardy.com> wrote:
> Bubbling seems like the answer to me. Put any listeners you need (change,
> select, click, etc.) on the form element, and then in your callback, use
> event.element() to get the source element. From there you can look at the
> new value.
>
> -Fred
>

kangax

unread,
Jun 10, 2008, 2:40:49 PM6/10/08
to Ruby on Rails: Spinoffs
I think this should be "fixed". Knowing which element was changed is
often crucial. I'll make a patch as soon as I get a chance. Meanwhile,
you can use this as a workaround:

new Form.Observer($(form), 0.3, (function(){
var previousValue = $(form).serialize(true), element;
return function(form, value) {
value = value.parseQuery();
for (var prop in value) {
if (value[prop] !== previousValue[prop]) {
element = $(form).down('[name=' + prop +']');
break;
}
}
previousValue = value;
// use "element" variable which references changed element
}
})());

Best,
kangax

louis w

unread,
Jun 10, 2008, 2:55:57 PM6/10/08
to Ruby on Rails: Spinoffs
Thanks kangax, nice to see this was already addressed.
Do you know if it is included in any of the stable releases?

In the meantime I will use your suggestion - which works perfectly.

Walter Lee Davis

unread,
Jun 10, 2008, 3:23:33 PM6/10/08
to rubyonrail...@googlegroups.com
I agree this would be nice on the Periodical Observer, but I think
what the initial reply was hinting at was this:

$('myform').observe('change',
function(evt){
alert(evt.element().id);
}
);

Walter

RobG

unread,
Jun 10, 2008, 7:46:07 PM6/10/08
to Ruby on Rails: Spinoffs


On Jun 11, 2:02 am, "Frederick Polgardy" <f...@polgardy.com> wrote:
> Bubbling seems like the answer to me. Put any listeners you need (change,

The change event doesn't bubble in the MS HTML DOM.


--
Rob

Frederick Polgardy

unread,
Jun 10, 2008, 8:05:37 PM6/10/08
to rubyonrail...@googlegroups.com
You know, I did run into this about three months back on a web project I was working on.  Extremely annoying indeed!  Thanks for the clarification.

kangax

unread,
Jun 10, 2008, 11:34:43 PM6/10/08
to Ruby on Rails: Spinoffs
This is not included anywhere yet.
In the upcoming release we try to focus on bugfixes (and general
polishing), so any enhancements will most likely have to wait for some
time.
I made a quick patch for this, but haven't had time to write unit
tests yet.
http://prototype.lighthouseapp.com/attachments/26787/0001-Form.Observer-to-pass-changed-element-to-a-callback.patch

- kangax

louis w

unread,
Jun 12, 2008, 2:53:49 PM6/12/08
to Ruby on Rails: Spinoffs
kangax, I noticed that if there are two inputs with the same name it
will always return the first occurance of the double item. Even if the
field you are updating is not one of the repeated items.
Don't know if this will impact the patch too.


On Jun 10, 11:34 pm, kangax <kan...@gmail.com> wrote:
> This is not included anywhere yet.
> In the upcoming release we try to focus on bugfixes (and general
> polishing), so any enhancements will most likely have to wait for some
> time.
> I made a quick patch for this, but haven't had time to write unit
> tests yet.http://prototype.lighthouseapp.com/attachments/26787/0001-Form.Observ...

louis w

unread,
Jun 12, 2008, 5:55:16 PM6/12/08
to Ruby on Rails: Spinoffs
Also, i am finding that it errors if your input name is an array. E.g.
foo[bar]

Do you know how to fix this. Should I apply the patch? Would that fix
it?

kangax

unread,
Jun 13, 2008, 1:47:43 PM6/13/08
to Ruby on Rails: Spinoffs
Does it error with patch applied?

- kangax

louis w

unread,
Jun 16, 2008, 5:18:32 PM6/16/08
to Ruby on Rails: Spinoffs
Did not work with the patch. Works with the change you suggested
$('foo').down('[name="'+ prop +'"]');

Can you add this to the patch so it's included in the next release?

Thanks so much for the help.
Reply all
Reply to author
Forward
0 new messages