Object mashup

1 view
Skip to first unread message

maalek

unread,
Dec 10, 2009, 12:21:49 PM12/10/09
to Prototype & script.aculo.us
Hello

I have problem with my Objects. My object have many props and method,
one of this is td (screen reprezentation of the object). I observe
click event to obj.td where I do many things on this object and change
td (screen reprezentation). My question : how find object where is
attach clicked td ?

Now I setAttribute to td something like obj_id, when td is clicked
first I read this attribute from td, find proper object by this id, do
what to do and finnaly change state of obj.td. This works fine but
when I have very many obj (like thousand), find object takes many time
(find object is longer then do what to do).


Here is simple example :

// example START

Event.observe(window, 'load', function() {
onLoad();
});

var objTrs = [];
var msg = null;

function onLoad(){

msg = document.createElement("div");
msg.innerHTML = "Click on cell";
$(document.body).appendChild(msg);

var table = document.createElement("table");
var tableBody = document.createElement("tbody");
table.appendChild(tableBody);
$(document.body).appendChild(table);
table.border="1"

for(var i = 0; i < 10; i++){
var myTr = {};
myTr.counter = 0;
myTr.tr = document.createElement("tr");
myTr.objTds = [];
objTrs.push(myTr);
for (var j = 0; j < 10; j++){
var myTd = {};
myTd.counter = 0;
myTd.td = document.createElement("td");
myTd.td.setAttribute('tr_id',i);
myTd.td.setAttribute('td_id',j);
myTd.td.innerHTML = myTr.counter + "_" + myTd.counter;
myTr.objTds.push(myTd);
myTr.tr.appendChild(myTd.td);

Event.observe(myTd.td, 'click', function() {
// find tr object in array
var tr_id = this.getAttribute("tr_id");
var myTr = objTrs[tr_id];
myTr.counter++;
// find proper td in myTr
var td_id = this.getAttribute("td_id");
var myTd = myTr.objTds[td_id];
myTd.counter++;
var msgTxt = "you click " + myTr.counter + " times on this TR
and " + myTd.counter + " on this TD";
myTd.td.innerHTML = myTr.counter + "_" + myTd.counter;
msg.innerHTML = msgTxt;
});

}
tableBody.appendChild(myTr.tr);
}

}

// example STOP

Best regards
maalek

julien Devouassoud

unread,
Dec 14, 2009, 10:48:05 AM12/14/09
to prototype-s...@googlegroups.com
Do you mean : hosw to find it's parent ?


2009/12/10 maalek <maa...@gmail.com>

--

You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group.
To post to this group, send email to prototype-s...@googlegroups.com.
To unsubscribe from this group, send email to prototype-scripta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.



Reply all
Reply to author
Forward
0 new messages