> var newLast = Element.recursivelyCollect(oldLast,'nextSibling')[0];

That did the trick. We sure do need an Element.bottom method, huh ;)


-Daniel


-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Colin Mollenhour
Sent: Monday, February 26, 2007 5:05 PM
To: [email protected]
Subject: [Rails-spinoffs] Re: Get Newly Inserted Element

Ahh! Two problems..

One, oldLast.next() will not work because you never extended oldLast. Even
if you wrapped that in $(), it wouldn't work if oldLast was a text node
because $() and Object.extend don't extend text nodes.

Two, since Prototype doesn't extend text nodes, and the dom traversal
functions call methods using the element extensions, it wouldn't work with
text nodes anyway..

  next: function(element, expression, index) {
    return Selector.findElement($(element).nextSiblings(), expression,
index);
  },
  nextSiblings: function(element) {
    return $(element).recursivelyCollect('nextSibling');
  }

So my example code *would* work if those were written without the extension
shortcuts like so:

  next: function(element, expression, index) {
    return Selector.findElement(Element.nextSiblings(element), expression,
index);
  },
  nextSiblings: function(element) {
    return Element.recursivelyCollect(element,'nextSibling');
  }

I believe these should all be changed to not use the shortcuts to make them
work in as many cases as possible, but I've had enough frustration as of
late trying to get one patch accepted that I'm not up to submitting another
one..

In the meantime, use this in place of Element.next(oldLast):

var newLast = Element.recursivelyCollect(oldLast,'nextSibling')[0];

Colin


Daniel Eben Elmore wrote: 
I must not be seeing something simple here:

var oldLast = $('websites-data').lastChild;
console.info(oldLast);
new Insertion.Bottom('websites-data',transport.responseText); 
var newLast = oldLast.next();
console.info(newLast);

The first console log returns a whitespace text node:
"\n      \n     "

The second console log is never recorded and the newLast variable is
unusable by my tests. A new element is being inserted, I can see it in the
DOM inspector. It looks like the next() method doesn't like moving off of a
whitespace node.




-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of Colin Mollenhour
Sent: Sunday, February 25, 2007 6:47 PM
To: [email protected]
Subject: [Rails-spinoffs] Re: Get Newly Inserted Element


If you are always inserting only one sibling like so:
var content = '<div>I am a sibling<span>I am the sibling's child so I 
don't matter</span></div>';
new Insertion.Bottom(element,content);

Then it is ok to use element.lastChild:
var newEl = element.lastChild; //the div you inserted above

However, if you have whitespace or text not wrapped in the node in your 
insertion content, this will not work. The safer method would be as in 
my previous example:

var oldLast = element.lastChild; //you don't care what this is, just stored
for later reference
new Insertion.Bottom(element,content);
var newEl = Element.next(oldLast); //gets the div you inserted

If the content you are inserting has multiple siblings, then the above code
would work to get the first element. TO get subsequent elements just keep
using Element.next:

//do something with the newEl from above (first element inserted)
while(newEl = Element.next(newEl)){
  //do something with each additional sibling element inserted
}

One caveat, if element is completely empty then storing oldLast will not
work because there is no lastChild. Could be solved like so:

if(!element.lastChild) element.appendChild(document.createTextNode());
var oldLast = element.lastChild; //now oldLast is guaranteed to exist
.... 

Or, if inserting one extraneous empty text node is something you have
problems with there are other solutions which I'll leave up to those who
care. <-(my disclaimer in case someone decides to pipe in their objections)


Colin


Daniel Eben Elmore wrote:
  
I'm primarily using Insertion.Bottom and don't see how to use the
    
traversal
  
methods because I don't know when I've reached the last element. I really
don't know anything about the element being inserted. It's an HTML snippet
returned from the server. Any ideas?
  
    





.

  



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to