1

我正在尝试在 javascript 中计算出一个迭代大约 10 个函数的函数。

我有一个函数可以创建服务以将 json url 调用到我的函数中。这与下面的 main 函数通信,该函数调用带有相应 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 使用下面的代码而不是在每个类别上都写出来我可以将变量传递给每个函数吗?我将不得不对每个类别进行这些 json 调用。

任何帮助将非常感激。

谢谢,马克

4

2 回答 2

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 必须是唯一的。

于 2012-09-19T18:53:51.137 回答
0

我不确定我是否完全理解问题所在,但您可能会从使用 [] 表示法访问 JavaScript 子项中受益不少。因此,例如, landp.Service.getCategoryTopTen()您可以打电话而不是打电话landp.Service["getCategoryTopTen"](),然后可以将其拉出,这样您就有了类似的东西:

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

然后你可以围绕它包装你的逻辑以迭代或以其他方式自动化。

于 2012-09-19T18:47:12.903 回答