2012-09-19 239 views
1

我正在嘗試找出一個函數,它可以在大約10個函數中進行迭代。使用jquery從另一個函數調用一個函數

我有一個函數創建服務來調用一個json url到我的函數中。這與主函數進行通信,在主函數下面調用服務函數,將相應url的選項用作啓動json請求的數據。請看下面;

var landp = { 
getCountryCode : function(){ 
    console.info('landp.GetCountryCode'); 
    return window.location.pathname.match(/\/([a-zA-Z]{2})/)[1].toLowerCase(); 
}, 
Service : { 
    getBaseUrl : function(){ 
     console.info('landp.Service.GetBaseUrl'); 
     return sprintf("/%s/service", landp.getCountryCode()) 
    }, 
    callService : function(method, options){ 
     console.info('landp.Service.CallService'); 
     var url = sprintf("%s/%s", landp.Service.getBaseUrl(), method); 
     $.ajax({ 
      url : url, 
      method : 'GET', 
      success : options.success, 
      error : options.error 
     }) 
    }, 
    getCategories : function(options){ 
     landp.Service.callService('category', options); 
    }, 
    getCategoryId : function(options){ 
     landp.Service.callService('category/id/13', options); 
    }, 
      getCategoryTopTen : function(options){ 
     landp.Service.callService('category/name/top-ten', options); 
    }, 
    getSeeMoreItems : function(options){ 
     landp.Service.callService('see-more', options); 
    }, 
    getPartnerOffers : function(options){ 
     landp.Service.callService('partner-offers', options); 
    } 
} 

}

這工作好,你可以看到getCategoryTopTen將調用認爲我需要使用以獲得正確的項目,如item.name,item.description等等等等JSON數據的URL 。代碼如下。

landp.Service.getCategoryTopTen({ 
    success : function(data){ 
     $('#header').after('<div id="cat-sub-options"></div>'); 
     $('#cat-sub-options').after('<div class="flexslider"><ul class="slides"></ul></div><div id="carousel-button" class="click"></div>');    
     $.each(data[0].items, function(i, item){ 
     $('.flexslider .slides').append('<li class=""><h5>' + i + '</h5><a href="' + item.url + '"><img src="' + item.imageLargeUrl + '" alt="' + item.name + ' image" /><span>' + item.name + '</span></a></li>'); 
    }); 
    }, 
    error : function(){ 
     console.log("Error getting categories"); 
    } 
}) 

正如你可以看到上面的代碼工作,它將正確的信息拉入正確的元素等。目前約有10個類別,如;

getCategoryTopTen getCategoryHistoryHeritage getCategoryWheretogo getCategoryShopping getCategorySportyPeople

正如你可以看到每一個將被分配給在JSON文件中的類URL。

我想要解決的是我如何做到以上所有的類別例如我們有:landp.Service.getCategoryTopTen與下面的代碼,而不是寫在每個類別itout我可以傳遞一個變量,而不是每個功能其他明智的我將不得不做每個類別的這些JSON調用。

任何幫助將不勝感激。

謝謝, 馬克

+0

請使用簡單的字符串與'+'運算符連接,而不是'sprintf'函數(它在內部執行相同的操作,甚至在使用正則表達式替換等時)。 JavaScript是一種高級語言:-) – Bergi

回答

0

我不知道我完全理解這個問題是什麼,但你可以使用[]符號訪問JavaScript的孩子們受益不少。因此,例如,而不是調用 landp.Service.getCategoryTopTen()你可以調用landp.Service["getCategoryTopTen"](),然後這可能再拉出所以你有這樣的:

var category="TopTen" 
var method="getCategory"+category 
land.Service[method](...) 

然後你可以環繞你的邏輯來遍歷或其他自動化。

+0

我不完全相信我理解這種方法? –

+0

在JavaScript中,您可以通過點符號'Object.function'或索引樣式符號'Object [「function」]'來訪問任何對象成員,因爲索引樣式使用一個字符串,您可以像處理其他字符串一樣操作該字符串。這仍然返回相同的對象引用。由於JS具有一流的功能,一旦你獲得了對象引用,你只需要通過添加'(args)'調用來調用它。如果你將它分解成行並將其想象爲:可能會更清晰: var method = Object [「method」];方法();' –

0

您可以遍歷方法名稱的數組,並每次使用相同的回調函數調用它們。入門的功能可以通過使用括號標記來完成:

landp.Service[methodname](function(){…}); 

不過,我會強烈建議不要定義爲系統中的每個類別額外的方法,但只是使類別名稱參數傳遞給函數:

… 
getCategoryById: function(id, options){ 
    landp.Service.callService('category/id/'+id, options); 
}, 
getCategoryByName: function(name, options){ 
    landp.Service.callService('category/name/'+name, options); 
}, 
… 

現在,循環在你的陣列更方便:

var cats = ["top-ten", "HistoryHeritage", "Wheretogo", "Shopping", "SportyPeople"]; 
for (var i=0; i<cats.length; i++) { 
    var catname = cats[i]; 
    landp.Service.getCategoryByName(catname, {…}); 
} 

順便說一句,你就需要從要素刪除的ID,因爲您將創建屆時間多次 - 但ID必須是唯一的。

+0

查看從json創建滑塊的部分是使事情運行緩慢的位。所以這就是爲什麼我不想用上面的代碼重複創建相同的功能。儘管我對這種方法有些困惑? –

+0

是的,如果您查詢多個服務並同時顯示結果,您的成功函數將需要一些適應性 – Bergi