【问题标题】:Run a function from within a each loop (ajax.response)从每个循环中运行一个函数(ajax.response)
【发布时间】:2013-04-08 08:55:40
【问题描述】:

我在尝试从每个循环中将数据传递给函数时遇到问题。我在下面大致粘贴了我的代码。基本上 functionA 获取 JSON 数据,将响应传递给 functionB。然后我想将每个响应项传递给 functionC,以便可以将它们添加到数组中以使用谷歌地图绘制标记。任何帮助,将不胜感激。谢谢

if(app == undefined) var app = {};

app.Application = function() {

this.functionA = function(){
        var self = this;

        var urlHash = location.hash;
    accessToken = urlHash.split('=')[1];

    if (!accessToken) { 
        return false; 
    } else {
        $.getJSON(instaAPIuri+"users/" + id + "/media/recent?access_token=" + accessToken + "&callback=?", self.functionB);
    };

};

this.functionB = function(response){
    var self = this;

    //Error codes
    if (response.meta.code == 400) {
        alert(response.meta.error_message);
    }

    //Create picture elements with basic information and image
    $.each(response.data, function (i, item) {

        //If item.location == null, while trying to get geolocation = error
        if (item.location != null) {
            functionC( item.location.latitude, item.location.longitude, item.images.thumbnail.url, item.user.username);
        }

    });

};

this.functionC = function(latitude, longitude, imgurl, user) {
    var self = this;
    var latLngPosition = new google.maps.LatLng(latitude, longitude);

    //Create marker with custom assets
    marker = new google.maps.Marker({
        position:latLngPosition,
        icon:   new google.maps.MarkerImage(imgurl,
                new google.maps.Size(110, 110),
                new google.maps.Point(0,0),
                new google.maps.Point(32, 32)),
        title: user,
        map:map
    });

    //Push in array to delete later
    markersArray.push(marker);
};

this.init();

};

$(function() {

var app = new app.Application();

});

【问题讨论】:

  • 也许尝试使用 self.functionC 而不是简单的 functionC?
  • 我试过了,但 self 指的是每个循环中的项目。 TypeError: self.functionC 不是函数
  • 我已经设法修复它。必须将匿名回调函数传递给 $.getJSON。它也会失败,因为我没有包含我的 else 语句

标签: javascript google-maps-api-3 each getjson


【解决方案1】:

functionB 是在 $.ajax() 设置对象的上下文中调用的。您可以使用 $.proxy() 将上下文更改为 app.Application:

  if(app == undefined) var app = {};

app.Application = function() {

this.functionA = function(){
        var self = this;

        var urlHash = location.hash;
    accessToken = urlHash.split('=')[1];

    if (!accessToken) { 
        return false; 
    } else {
        $.getJSON(instaAPIuri+"users/" + id + "/media/recent?access_token=" + accessToken + "&callback=?", $.proxy(self.functionB, self));
    };

};

this.functionB = function(response){
    var self = this;

    //Error codes
    if (response.meta.code == 400) {
        alert(response.meta.error_message);
    }

    //Create picture elements with basic information and image
    $.each(response.data, function (i, item) {

        //If item.location == null, while trying to get geolocation = error
        if (item.location != null) {
            self.functionC( item.location.latitude, item.location.longitude, item.images.thumbnail.url, item.user.username);
        }

    });

};

this.functionC = function(latitude, longitude, imgurl, user) {
    var self = this;
    var latLngPosition = new google.maps.LatLng(latitude, longitude);

    //Create marker with custom assets
    marker = new google.maps.Marker({
        position:latLngPosition,
        icon:   new google.maps.MarkerImage(imgurl,
                new google.maps.Size(110, 110),
                new google.maps.Point(0,0),
                new google.maps.Point(32, 32)),
        title: user,
        map:map
    });

    //Push in array to delete later
    markersArray.push(marker);
};

this.init();

};

$(function() {

var app = new app.Application();

});

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2018-12-27
    • 2019-11-16
    • 1970-01-01
    • 2020-12-27
    • 2011-05-14
    • 2020-12-24
    • 2012-10-07
    • 2021-02-23
    相关资源
    最近更新 更多