going through pages...

2 views
Skip to first unread message

Manuel Alonso

unread,
Jan 12, 2008, 6:55:19 AM1/12/08
to Lista Google-AJAX-Search-API
Hi!
What I'm trying to do is saving into an array the results of every page...  is it possible? how?

what I'm doing is this (but it does't change the page! it's save):
In the "searchComplete" function:

   //for every page
  for(var p=0; p<searcher.cursor.pages.length; p++)
  {
     methodClosure(this, this.gotoPage,[searcher, p]);
     //for every result
     for (var i=0; i<searcher.results.length; i++)
     {
        resultados.push(searcher.results[i]); //"resultados" is my array
     }
  }

NOTE: I'm using the "raw search" sample, but just for web searches.

problems: it never changes from page 1. If there are 4 pages, it saves 4 times each result of page 1! ¡¿why?!
help is welcome!
thanks!

jgeerdes [AJAX APIs "Guru"]

unread,
Jan 13, 2008, 6:38:47 AM1/13/08
to Google AJAX API
By putting the gotoPage command inside a straight loop and trying to
assign the results to an array at the same time, you are skipping
pages without giving the searcher a chance to actually get the
results. You need to use the searcher's setSearchCompleteCallback
method to call a function that will store the results into an array,
and then EITHER skip to the next page if the current page index is
less than the last page OR finish processing the results (e.g., by
displaying them to the page or sending them back to the server, etc.)
if you've got all the pages.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

Ben

unread,
Mar 5, 2008, 4:23:32 PM3/5/08
to Google AJAX API
I'm also trying to understand this...

So setSearchCompleteCallback() is used to register a particular
function, which is called whenever a search is completed. We might
call that function onSearchComplete().

And you are saying that a new search has to be performed for each page
of results.

So within onSearchComplete() we need to see if we are already at the
last page of results, and if not, increment with gotoPage().

And presumably we need to tell the searcher to execute again, but only
if it's not the last page. And we'd do that in a loop in the
onSubmit() function (i.e. a function we set with
setOnSubmitCallback()).

So our onSubmit() function would need to call the execute function 4
times, if we want to get all 4 pages of results... is that right?



On Jan 13, 11:38 am, "jgeerdes [AJAX APIs \"Guru\"]"
> jgeer...@mchsi.com

jgeerdes [AJAX APIs "Guru"]

unread,
Mar 5, 2008, 9:54:42 PM3/5/08
to Google AJAX API
Well, pretty much. But you're making it a little harder than it needs
to be. It works like this:

onSubmit handler processes input, executes search

onSearchComplete (set using setSearchCompleteCallback()) processes
search results, assigns results items to second variable (VERY
important) and/or appends CLONES (VERY important) of html nodes to
results div. Checks to see if last page of results; if not, calls
gotoPage(nextIndex);

onSearchComplete (set using setSearchCompleteCallback()) processes
search results, assigns results items to second variable (VERY
important) and/or appends CLONES (VERY important) of html nodes to
results div. Checks to see if last page of results; if not, calls
gotoPage(nextIndex);

onSearchComplete (set using setSearchCompleteCallback()) processes
search results, assigns results items to second variable (VERY
important) and/or appends CLONES (VERY important) of html nodes to
results div. Checks to see if last page of results; if not, calls
gotoPage(nextIndex);

onSearchComplete (set using setSearchCompleteCallback()) processes
search results, assigns results items to second variable (VERY
important) and/or appends CLONES (VERY important) of html nodes to
results div. Checks to see if cursor.currentPageIndex >=
cursor.pages.length; if not, calls gotoPage(nextIndex);

====

So you only call execute() once, but your onSearchComplete calls
gotoPage() if necessary, which loops back to onSearchComplete, which
calls gotoPage() if necessary, etc., etc., etc. I.e., gotoPage()
takes care of the subsequent execute()'s for you.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jgee...@mchsi.com

If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!

Ben

unread,
Mar 6, 2008, 12:42:09 PM3/6/08
to Google AJAX API
OK I get it, finally. onSearchComplete() is recursive, which is why it
gets tricky.

You can see this working at http://gfaster.com/more/

Thanks for the help. I do think the documentation could be a little
bit clearer about this.


On Mar 6, 2:54 am, "jgeerdes [AJAX APIs \"Guru\"]"
> jgeer...@mchsi.com
Reply all
Reply to author
Forward
0 new messages