Bill Katz
2005-Sep-15 13:28 UTC
[Rails-spinoffs] Javascript object literals -- prototype clash?
I''ve been playing with some ideas in getting the server-side Rails code
and
client-side AJAX stuff working nicely together while still providing
fallback for non-js viewers. I''m using Behaviour + prototype.
I was experimenting with using an object literal to define forms that would
be inserted into appropriate divs. Why not just write the forms on the
server side and unhide them when needed? Well, if it''s a large table of
information, and I want to be able to click and change any part, it makes
sense to define characteristics of forms and then insert them client-side
when needed. (No need for 40+ hidden forms.)
Anyway, I''m trying to use a javascript object literal definition that
looks
something like this:
var forms = {
form1: {
input_line1: {
name: ''stuff1'',
size: ''30'',
type: ''text'',
value: ''Init string for stuff1''
},
input_line2: {
name: ''stuff2'',
size: ''30'',
type: ''text'',
value: ''Init string for stuff2''
}
},
form2: {...}, ...
}
Within the form generation javascript routine, I have:
var input_list = forms[form_type];
for (var i in input_list) {
form = form + ''<input '';
var item = input_list[i];
for (var prop in item) {
form = form + prop + ''="'' + item[prop] + ''"
'';
}
form = form + ''/><br />'';
}
If I have form_type = ''form1'', this will create a form string
with <input>
tags for input_line1, input_line2, etc.
Now this mostly works, but there seems to be some object pollution. Instead
of just getting the stuff in my object literal definition, I''m getting
an
"extend" property that throws in:
extend="function(object){ return
Object.extend.apply(this,[this,object]);}"
for each of my inputs. Also, there''s an undefined object in the outer
for..in loop that has "bind", "bindAsEventListener", and the
"extend"
properties. I think it''s prototype extending the base Object (not sure
about
the extra undefined object in my input list, though). Since I''m a
relative
javascript/prototype/Behaviour) newbie, I''m clueless in how I would fix
this, other than labeling properties "wt_" and then screening for that
prefix.
Also, feel free to slap me upside the head and say, "if you want to do
that,
you should REALLY be doing this..." :) The long-term idea is to define
forms
and workflow one place (DRY) and have Rails generate both AJAX and non-AJAX
ways of presentation.
Your thoughts are appreciated.
-Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails-spinoffs/attachments/20050915/2dafbfce/attachment.html
Martin Bialasinski
2005-Sep-15 13:56 UTC
[Rails-spinoffs] Javascript object literals -- prototype clash?
On 15/09/05, Bill Katz <billkatz@gmail.com> wrote:> Now this mostly works, but there seems to be some object pollution. Instead > of just getting the stuff in my object literal definition, I''m getting an > "extend" property that throws in: > > extend="function(object){ return Object.extend.apply(this,[this,object]);}"Update to the latest scriptaculous pre version. Its prototype.js has this thing corrected. Bye, Martin