2013-04-07 31 views
1

我在嘗試從每個循環內將數據傳遞給函數時遇到問題。我粗略地粘貼了下面的代碼。基本上,函數A獲取JSON數據,並將響應傳遞給函數B.然後,我想將每個響應項傳遞給functionC,以便將它們添加到數組中以使用Google地圖繪製標記。任何幫助,將不勝感激。謝謝從每個循環內運行函數(ajax.response)

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(); 

}); 
+0

也許嘗試使用self.functionC而不是簡單的functionC? – 2013-04-07 21:42:06

+0

我試過,但自我指的是在每個循環中的項目。 TypeError:self.functionC不是函數 – jjkilpatrick 2013-04-07 21:59:29

+0

我設法修復它。必須將匿名回調函數傳遞給$ .getJSON。另外它會失敗,因爲我沒有包含我的else語句 – jjkilpatrick 2013-04-07 23:43:29

回答

1

函數B被$ .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(); 

});