2014-04-26 37 views
1

我正在製作一個新聞閱讀器應用程序,並使用Parse.com後臺作業來收集報紙RSS源的鏈接。我使用xmlreader.js和sax.js來解析httpResponse並使用saveAll和beforeSave定期更新數據瀏覽器中的類。如何將背景作業轉換爲類似功能的東西?

我有多種類別的報紙,總共有30多對(我想後面會包括更多的對,因爲我想包括地區報紙)。到目前爲止,我一直在與一家報紙和一個類別合作 - 印度教體育類;現在它工作正常。製作這兩個函數的副本並創建工作不會高效,我認爲。

因此,我想問我是否可以轉換這兩個工作,然後保存到某種功能,以便我可以只傳遞任何報紙類別對類名或其URL自動執行的東西。

全碼 - main.js

工作 -

Parse.Cloud.job("job_hindu_sports", function (request, response) { 
return Parse.Cloud.httpRequest({ 
    url: 'http://www.thehindu.com/sport/?service=rss' 
}).then(function(httpResponse) { 
    var someXml = httpResponse.text; 
    xmlreader.read(someXml, function (err, res){ 
     if(err) { 
      response.error("Error " +err); 
      return console.log(err); 
     } 

     var listArray = []; 
     res.rss.channel.item.each(function (i, item){ 
      var hinduSports = new HinduSports(); //@startswithaj - this part 
      hinduSports.set("link", item.link.text()); 
      hinduSports.set("title", item.title.text()); 
      hinduSports.set("pubDate", item.pubDate.text()); 
      //console.log("pubDate - "+ item.pubDate.text()); 
      listArray.push(hinduSports); 
     }); 

     var promises = []; 
     Parse.Object.saveAll(listArray, { 
       success: function(objs) { 
        promises.push(objs); 
        console.log("SAVED ALL!"); 
       }, 
       error: function(error) { 
        console.log("ERROR WHILE SAVING - "+error); 
       } 
      }); 
     return Parse.Promise.when(promises);   

    }); 
}).then(function() { 
     response.success("Saving completed successfully."); 
     },function(error) { 
     response.error("Uh oh, something went wrong."); 
}); 
}); 

beforeSave -

Parse.Cloud.beforeSave("HinduSports", function(request, response) { 
//console.log("in beforeSave"); 
var query = new Parse.Query(HinduSports); 
var linkText = request.object.get("link") 
var titleText = request.object.get("title"); 
query.equalTo("link", linkText); 
query.first({ 
    success: function(object) { 
    //console.log("in query"); 
    if (object) { 
     //console.log("found"); 
     if(object.get('title')!==titleText){ 
      console.log("title not same"); 
      object.set("title", titleText); 
      response.success(); 
     } 
     else{ 
      console.log("title same"); 
      response.error(); 
     } 
    } else { 
     console.log("not found"); 
     response.success(); 
    } 
    }, 
    error: function(error) { 
    response.error(); 
    } 
}); 
}); 

回答

1

在你的工作的代碼可以查詢您的數據存儲爲所有要處理的URL,並然後遍歷請求每個url的結果並將httpresponse傳遞給執行所有工作的函數

所以你必須(僞代碼)

function getDataForNewspaper(id, url){ 
    return (function(id) { 
     Parse.Cloud.httpRequest({ 
      url: url 
     }).then(function(httpResponse){ 
      processDataForNewspaper(id, httpResponse) 
     }) 
    })(id) //you need to have this in a closure so you can pass id to processDataFor... 

function processDataforNewpaper(id, httpResponse){ 
    someXml = httpResponse.text 
    //process your xml here 
} 

Parse.Cloud.job("get_data_for_all_newspapers", function (request, response) { 
    var query = new Parse.Query("Get all the newspapers").find{ 
     success: function(list){ 
     for each newspaper in list then 
       getDataForNewspaper(newspaper.id, newspaper.url) 
     } 
    } 
} 

這是不是最好的解釋,但我希望這有助於

+0

感謝您的回覆。我有一些疑問.___ 1.你認爲'(id)//你需要把它放在一條線中是什麼意思?它是一個字符串,它等於我正在處理的報紙的子類名稱?___ 2。 DUring XML的過程我需要一個子類的實例來添加數據。不是嗎?____ 3.在處理XML之後,我將如何使用beforeSave?我應該通過id作爲第一個參數嗎? –

+0

您可能最好在parse.com論壇上發佈您的問題。他們會有更多的經驗分析用戶來幫助你。 – startswithaj

+1

我在那裏遇到了一個糟糕的經歷,沒有人回覆。 –

0

與@startswithaj幫助我修改代碼來保存所有的文章在一類。唯一剩下的就是添加一個beforeSave方法。但仍然存在問題。有時只能完成saveAll。例如。我跑的代碼第一次在日誌中得到這個:刪除一些無用的console.log我這後

I2014-04-26T18:18:40.036Z] v93: Ran job job_get_data_for_all_newspapers with: 
Input: {} 
Result: Saving completed successfully. 
I2014-04-26T18:18:40.926Z] Successfully retrieved 2 
I2014-04-26T18:18:40.926Z] getData NEW & CAT ID - 1, 5 feedUrl http://www.thehindu.com/sport/?service=rss 
I2014-04-26T18:18:40.927Z] getData NEW & CAT ID - 1, 4 feedUrl http://www.thehindu.com/news/national/?service=rss 
I2014-04-26T18:18:40.927Z] promisesGetNP [object Object],[object Object] 
I2014-04-26T18:18:41.479Z] processData NEW & CAT ID - 1, 5 
I2014-04-26T18:18:41.622Z] listArray http://www.thehindu.com/sport/other-sports/mankirat-singh-sets-record/article5951540.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication 
I2014-04-26T18:18:41.628Z] promises undefined 
I2014-04-26T18:18:41.629Z] promisesGetData 
I2014-04-26T18:18:41.629Z] Done getData? 
I2014-04-26T18:18:42.082Z] processData NEW & CAT ID - 1, 4 
I2014-04-26T18:18:42.311Z] listArray http://www.thehindu.com/news/national/muslim-women-entitled-to-maintenance-even-after-divorce-supreme-court/article5951562.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication 
I2014-04-26T18:18:42.324Z] promises undefined 
I2014-04-26T18:18:42.324Z] promisesGetData 
I2014-04-26T18:18:42.324Z] Done getData? 
I2014-04-26T18:18:42.324Z] done job 

和第二次。你可以看到有一個SAVED ALL!被稱爲在saveAllsuccess: function -

I2014-04-26T18:20:53.130Z] v94: Ran job job_get_data_for_all_newspapers with: 
Input: {} 
Result: Saving completed successfully. 
I2014-04-26T18:20:53.307Z] Successfully retrieved 2 
I2014-04-26T18:20:53.307Z] getData NEW & CAT ID - 1, 5 feedUrl http://www.thehindu.com/sport/?service=rss 
I2014-04-26T18:20:53.307Z] getData NEW & CAT ID - 1, 4 feedUrl http://www.thehindu.com/news/national/?service=rss 
I2014-04-26T18:20:53.911Z] processData NEW & CAT ID - 1, 5 
I2014-04-26T18:20:53.951Z] listArray http://www.thehindu.com/sport/other-sports/mankirat-singh-sets-record/article5951540.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication 
I2014-04-26T18:20:53.995Z] Done getData? 
I2014-04-26T18:20:54.200Z] SAVED ALL! 
I2014-04-26T18:20:54.818Z] processData NEW & CAT ID - 1, 4 
I2014-04-26T18:20:55.016Z] listArray http://www.thehindu.com/news/national/muslim-women-entitled-to-maintenance-even-after-divorce-supreme-court/article5951562.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication 
I2014-04-26T18:20:55.031Z] Done getData? 
I2014-04-26T18:20:55.031Z] done job 

我的新代碼可以發現here。新代碼從150行開始。

相關問題