Forum: Rails Spinoffs (closed, excessive spam) Best way to react to form changes...

54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-10 17:23
(Received via mailing list)
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.
F490e41cd5de1393a9f6958b70dae6ab?d=identicon&s=25 Frederick Polgardy (Guest)
on 2008-06-10 18:02
(Received via mailing list)
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 <louiswalch@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.
54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-10 19:34
(Received via mailing list)
Are you saying I need to assign a listener to each input field?
F1fcf834ddad415f60d29c87cc10d4d4?d=identicon&s=25 kangax (Guest)
on 2008-06-10 20:43
(Received via mailing list)
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
54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-10 20:56
(Received via mailing list)
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.
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2008-06-10 21:24
(Received via mailing list)
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
F2c683e7dc0c4d4bcc790f87eaa67301?d=identicon&s=25 RobG (Guest)
on 2008-06-11 01:46
(Received via mailing list)
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
F490e41cd5de1393a9f6958b70dae6ab?d=identicon&s=25 Frederick Polgardy (Guest)
on 2008-06-11 02:06
(Received via mailing list)
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.

On Tue, Jun 10, 2008 at 6:46 PM, RobG <rgqld@iinet.net.au> wrote:

>
> 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.


--
Science answers questions; philosophy questions answers.
F1fcf834ddad415f60d29c87cc10d4d4?d=identicon&s=25 kangax (Guest)
on 2008-06-11 05:35
(Received via mailing list)
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/267...

- kangax
54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-12 20:54
(Received via mailing list)
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.
54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-12 23:55
(Received via mailing list)
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?
F1fcf834ddad415f60d29c87cc10d4d4?d=identicon&s=25 kangax (Guest)
on 2008-06-13 19:48
(Received via mailing list)
Does it error with patch applied?

- kangax
54c4985debe9ede54b95597ada6532fa?d=identicon&s=25 louis w (Guest)
on 2008-06-16 23:19
(Received via mailing list)
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.
This topic is locked and can not be replied to.