views:

126

answers:

1

Say we have an XML document with many book nodes...

When parsing XML with jquery, how can i pass the current node from each() iteration to another function that will do some stuff until something is reached and then go back to the previous function (passing along the current node from this function back to the first function)?

Here something more descriptive (this is just an example out of my head, not accurate):

function MyParser(x1,x2,dom)
{
  // if i am called by anotherFunction(thisNode) proceed from the passed node

  dom.find('book').each(function()
  {
    var Letter = thisNode.find(author).charAt(0);
    if(x1 == Letter)
    {
      // print everything till the next letter (x2)
      anotherFunction(thisNode)
    }

  }
}

function anotherFunction(x2,thisNode)
{
  //continue parsing here until you reached x2
  //when x2 is reached, return to previous function passing again the current node
}
A: 

Something like this? Your node will still be on scope.

var f1=function(node){
  // do stuff
}

var f2=function(node){
  // do stuff with node
  $('query').each(
    function(){
      f1(node);
    }
  );
  // continue doing stuff with node
}

If you want to be explicit you could have f1 return node:

var f1=function(node){
  // do stuff
  return node;
}

Correct me if I am misunderstanding. I am not sure if you want to return to the first function or call the first function again.

Jeremy
Thank you that helps with passsing the current node. But what i actually want inside f2() is to continue parsing from the position where node was in f1() before calling f2().f1() each() //do stuff //call f2(node) - sending the current node f2(node) each(begin from node) //do stuff // call f1(node) sending current nodeAND NOW F1() needs to continue from the node that has been send from F2().
Johusa
f1(node) if no node exists (nothing f2 has returned) parse from the beginning (this happens only the first time) with: each()if node exists (f2 did send us back a node, continue parsingfrom that position: each(from node)a) do stuff till something is reached (eg: author name)b) call f2(node) - passing current node from within each()--------------------------------------------------------------f2(node)each(from node)a) do stuff till something is reached (eg: next author name)b) call f1(node) - passing current node back to previous function so that it can continue
Johusa
Honestly I can't make heads or tails of what you want. Maybe if you explain what you want to accomplish then we can help design an algorithm for you.
Jeremy