Created
February 14, 2013 19:40
-
-
Save agustinhaller/4955690 to your computer and use it in GitHub Desktop.
defferred example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var scraper = (function(){ | |
| var scrape_retailer = function(retailer, product_keyword){ | |
| var urlGet = retailer.endpoint.replace("{{KEYWORD}}",product_keyword), | |
| retailer_products = [], | |
| $dfd = $.Deferred(); | |
| appAPI.request.get(urlGet, | |
| function(response, responseHeaders) | |
| { | |
| $(response).find(retailer.container).each(function(index, elem){ | |
| var product_price = $(elem).find(retailer.product_price).text().trim(), | |
| product_name = $(elem).find(retailer.product_name).text().trim(), | |
| product_url = $(elem).find(retailer.product_url).attr("href").trim(); | |
| retailer_products.push({price:product_price,name:product_name,url:product_url}); | |
| }); | |
| $dfd.resolve(retailer_products); | |
| }, | |
| function(e) | |
| { | |
| console.log('Unable to get url ' + urlGet); | |
| $dfd.reject({status:"ERROR", msg:'Unable to get url ' + urlGet}); | |
| } | |
| );// END CORS CALL | |
| return $dfd.promise(); | |
| }; | |
| return{ | |
| scrape : function(retailers, product_keyword){ | |
| retailers.each(function(index, retailer){ | |
| console.log(retailer); | |
| var $retailer_promise = scrape_retailer(retailer, product_keyword); | |
| // This handles a success retailer scraping | |
| $retailer_promise.done(function(retailer_products){ | |
| console.log("THESE ARE RETAILER PRODUCTS", retailer_products); | |
| // Here we can add them to the 'TravelHelper' top bar, or just join them with other retailer products, | |
| // but we don't have to wait until all retailer products are done, we can start working with these ones | |
| // right here | |
| // all_products.push({retailer:retailer.name,products:retailer_products}); | |
| }); | |
| // This handles an error when scraping the retailer | |
| $retailer_promise.fail(function(error_response){ | |
| console.log("ERROR WHEN SCRAPING THE RETAILER", error_response); | |
| }); | |
| });// END FOREACH RETAILER | |
| }// END scrape function | |
| } | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment