Forum: Rails Spinoffs (closed, excessive spam) cqan i chain this code together in prototype?

Posted by elduderino (Guest)
on 2008-07-01 17:09
(Received via mailing list)
Hi,

 I had this code:
 var newCopy = new Element('div', { 'id' : obj.id, 'class' :
'de_small' }).update(obj.innerHTML);

which worked fine but now i add this and it breaks my script...it's
hard to explain exactly how it breaks but it involves a draggable
object not being droppable anymore

var newCopy = new Element('div', { 'id' : obj.id, 'class' :
'de_small' }).update(obj.innerHTML).observe('click', deleteThis());

can i not do it like this?
Posted by Frederick Polgardy (Guest)
on 2008-07-01 17:29
(Received via mailing list)
I don't see where the new element is being inserted into the document. 
Is
there code we're missing?

-Fred

On Tue, Jul 1, 2008 at 10:08 AM, elduderino 
<jamesfiltness@googlemail.com>
wrote:

>
> var newCopy = new Element('div', { 'id' : obj.id, 'class' :
> 'de_small' }).update(obj.innerHTML).observe('click', deleteThis());
>
> can i not do it like this?


--
Science answers questions; philosophy questions answers.
Posted by elduderino (Guest)
on 2008-07-01 17:35
(Received via mailing list)
HI There,

Yes the new element is being inserted later ( $
(dropZoneCount.id).appendChild(newCopy);)....that's all working
fine....i only posted the relevant code....the bit of code when it was
working and then the same bit of code with the extra code taht breaks
the script. Can you see anything wrong with it?
Posted by Matt Foster (Guest)
on 2008-07-01 21:10
(Received via mailing list)
Looking at this brief snippet I see one potential error,  You're
executing your callback, try sending just the reference.
Posted by Frederick Polgardy (Guest)
on 2008-07-01 21:20
(Received via mailing list)
In other words, change:

.observe('click', deleteThis());

To:

.observe('click', deleteThis);

You're passing deleteThis() as the value of your callback, which isn't
a function, but the return value of calling it.

-Fred
Posted by elduderino (Guest)
on 2008-07-02 13:15
(Received via mailing list)
Yes taht's it....why the hell didn't it try that!

Thanks
Posted by elduderino (Guest)
on 2008-07-02 17:20
(Received via mailing list)
Ah What about in the event that i need to feed a parameter to the
function??
 like: .observe('click', deleteThis(param));

I wouldn't mind just running the funcion from here:

 var newCopy = new Element('div', { 'id' : obj.id, 'class' :
'de_small' }).update(obj.innerHTML).observe('click', function{(...)};

(so just have the function code just after click...like above) ...but
i don't know if you can do it/get the syntax right
Posted by kangax (Guest)
on 2008-07-02 17:47
(Received via mailing list)
Just wrap in another function:

.observe('click', function(){ deleteThis(param) });

-- kangax
Posted by Frederick Polgardy (Guest)
on 2008-07-02 17:55
(Received via mailing list)
Or:

.observe('click', deleteThis.curry(param));

-Fred

On Jul 2, 2008, at 10:46 AM, kangax wrote:

>
> Just wrap in another function:
>
> .observe('click', function(){ deleteThis(param) });
>
> -- kangax

--
Science answers questions; philosophy questions answers.
Posted by kangax (Guest)
on 2008-07-02 18:40
(Received via mailing list)
Sure, if performance is not in question : )

-- kangax
Posted by elduderino (Guest)
on 2008-07-03 14:08
(Received via mailing list)
OK, excellent didn't know you could do that!
This topic is locked and can not be replied to.