Problems with $('formID').serialize(true).merge({foo:'bar'}).

68 views
Skip to first unread message

Richard Quadling

unread,
Feb 17, 2009, 10:35:21 AM2/17/09
to prototype-s...@googlegroups.com, prototy...@googlegroups.com
Hello.

I'm having problems using the Hash.merge() method on a Form.serialize(true).


What I want to do is ...

parameters : $('formUserAdmin').serialize(true).merge({Action :
'ClientAddUser', JSONP:'tabUA_UpdateUserIDs'});

But it isn't working because the output of the serialize method is not
a true Hash, like you would get from $H().

Using ...

parameters : $H($('formUserAdmin').serialize(true)).merge({Action :
'ClientAddUser', JSONP:'tabUA_UpdateUserIDs'});

is working as I would expect.

But I don't know if I should be doing that as I was expecting the
serialize() method to return a hash.


As always, there are 2 fixes.

1 - Correct the documentation to reflect the behaviour.
2 - Fix the code.


I think the fix is in src/dom/form.js, line 28 which currently reads ...

return options.hash ? data : Object.toQueryString(data);

and maybe should read ...

return options.hash ? $H(data) : Object.toQueryString(data);


Now, if a user is already $H() the form.serialize(true), then this
still seems to be OK.

$H($H($H($H($H($('formUserAdmin').serialize(true)).merge({Foo:'bar'}))))).inspect()

provides the same output as ...

$H($('formUserAdmin').serialize(true)).merge({Foo:'bar'}).inspect()

which, with the patch applied is now the same as ...

$('formUserAdmin').serialize(true).merge({Foo:'bar'}).inspect()

With this patch, the documentation now agrees with the code.


In looking at the unit tests, I think the reason this has not been
seen is in unittest.js

function assertHashEqual(expected, actual, message) {
expected = $H(expected);
actual = $H(actual);
var expected_array = expected.toArray().sort(), actual_array =
actual.toArray().sort();
message = buildMessage(message || 'assertHashEqual', 'expected:
<?>, actual: <?>', expected, actual);
// from now we recursively zip & compare nested arrays
function block() {
return expected_array.length == actual_array.length &&
expected_array.zip(actual_array).all(assertPairEqual);
}
this.assertBlock(message, block);
}

Both the expected and actual are converted to $H()'d. Which means they
will match.

Only the expected should be $H()'d as it the actual should already be
$H()'d by prototype.

I hope that makes sense and is right, if not, I'll stick with $H()-ing
my serialized form.

Regards,

Richard.


--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

Tobie Langel

unread,
Feb 17, 2009, 11:28:47 AM2/17/09
to Prototype: Core
HI Richard,

That's a documentation issue.

Expected output is a vanilla JS object, not a hash.

Mind filing a ticket in LH ?

Best,

Tobie

Tobie Langel

unread,
Feb 17, 2009, 11:48:03 AM2/17/09
to Prototype: Core

Tobie Langel

unread,
Feb 17, 2009, 11:55:48 AM2/17/09
to Prototype: Core
Oh... and please avoid cross-posting next time ;)

Best,

Tobie

Richard Quadling

unread,
Feb 17, 2009, 12:39:55 PM2/17/09
to prototy...@googlegroups.com
2009/2/17 Tobie Langel <tobie....@gmail.com>:
>
> Oh... and please avoid cross-posting next time ;)
>
> Best,
>
> Tobie
> >
>

Sorry about the cross posting. It started as an issue with serialize
and then became a maybe bug issue.

Tobie Langel

unread,
Feb 17, 2009, 12:44:21 PM2/17/09
to Prototype: Core
no problem.

Best,

Tobie

On Feb 17, 6:39 pm, Richard Quadling <rquadl...@googlemail.com> wrote:
> 2009/2/17 Tobie Langel <tobie.lan...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages