2012-09-09 30 views
0

我正在使用一個句柄模板併成功註冊了一個幫助程序。但是,我想要做一個$ .getJSON,並從模板中顯示該ajax請求顯示一些結果。使用Handlebars Helper中的函數

這裏是我的javascript代碼(寫在咖啡)

 Handlebars.registerHelper('getNearestAddressFromPoint',(lat,lon) -> 
    console.log("in register helper") 
    bingURL = 'http://dev.virtualearth.net/REST/v1/Locations/' + lat + ','+ lon + '?&key=' + bingMapsKey + '&jsonp=?' 
    $.getJSON(bingURL,@pointSuccess)) 


    pointSuccess:(data)=> 
    tooltipAddr = $(@el).find("#tooltipAddr") 
    address = data.resourceSets[0].resources.name 
    $(tooltipAddr).text(address) 
    console.log("hello") 

這jquery.text沒有運行,也不是執行console.log,但「註冊幫手」正在記錄。

這可能是因爲幫助程序正在返回$ .getJSON作爲函數,因爲在我的模板中[object Object]正在顯示,並且它正在將函數顯示爲對象。我希望它能夠返回回叫的結果,或者回撥被稱爲

回答

0

線索位於url的末尾:'&jsonp=?'。這表明使用JSONP,這是一種使用JSON跨域的方式。因此,而不是$.getJSON你將不得不使用這樣的:

$.ajax({ 
    url: bingURL, 
    dataType: 'jsonp', 
    jsonp: "jsonp", 
    success: @pointSuccess 
}); 

取消對您的網址的'&jsonp=?',讓jQuery的處理。

相關問題