【问题标题】:Using function inside Handlebars Helper在 Handlebars Helper 中使用函数
【发布时间】:2012-09-09 00:32:18
【问题描述】:

我正在使用车把模板并成功注册了一个助手。但是,我想做一个 $.getJSON 并在模板中显示该 ajax 请求的一些结果。

这是我的 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 也没有运行,但是正在记录“in register helper”。

这可能是因为帮助程序将 $.getJSON 作为函数返回,因为在我的模板中 [object Object] 正在显示并且它正在将函数显示为对象。我想要它以便返回回调的结果,或者正在调用回调

【问题讨论】:

    标签: javascript jquery coffeescript bing-maps handlebars.js


    【解决方案1】:

    线索在网址的末尾:'&jsonp=?'。这表明使用了JSONP,这是一种使用JSON跨域的方式。因此,您将不得不使用以下内容,而不是 $.getJSON

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

    删除你的url上的'&jsonp=?',让jQuery处理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2013-01-27
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多