Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js Can';异步函数主干中的t集模型_Backbone.js_Coffeescript - Fatal编程技术网

Backbone.js Can';异步函数主干中的t集模型

Backbone.js Can';异步函数主干中的t集模型,backbone.js,coffeescript,Backbone.js,Coffeescript,我有这个观点 class FoursquareSearch.Views.Origin extends Backbone.View events: 'change [name=origin]': 'setOrigin' 'click [name=geolocate]' : 'geolocate' geolocate: -> navigator.geolocation.getCurrentPosition(@handle) handle: (respons

我有这个观点

class FoursquareSearch.Views.Origin extends Backbone.View

events:
    'change [name=origin]': 'setOrigin'
    'click [name=geolocate]' : 'geolocate'

  geolocate: ->
    navigator.geolocation.getCurrentPosition(@handle)

  handle: (response) ->
    @model.set(coords: response)  
我试图确定设备的位置,然后用响应设置模型。不管我得到什么

Uncaught TypeError: Cannot call method 'set' of undefined 
奇怪的是,只有在函数内部才会发生这种情况。例如,如果我使用:

  geocode: (location) ->
    data = 
      location: location

    $.ajax(
      type: 'POST'
      url: '/search/geocode'
      data: data
      dataType: 'json'

      error: (jqXHR, textStatus, errorThrown) =>
        alert("ERROR")


      success: (response, text, xhr) =>
        @model.set(coords: response)
        @center(@model.get('coords'))
        )
在同一个视图中,它可以工作,而且工作得很好。。。但是,我就是无法使用其他函数来设置模型。我认为这是因为它是异步的。我决不是这方面的专家,我一路走下去就有了脊梁骨,但这让我很难堪

没有为
getCurrentPosition
回调函数指定任何特定的上下文,因此
回调函数中的
可能是
窗口
<代码>窗口
通常没有
模型
属性,因此:

handle: (response) ->
  @model.set(coords: response)
getCurrentPosition
调用它时,结果如下:

handle: (response) ->
  window.model.set(coords: response)
因此,
handle
尝试在不存在的
窗口上调用
set
。model
存在您的
无法调用未定义的方法“set”的错误

尝试将
句柄定义为:

您的其他
@model.set
调用工作正常,因为
@
是您的视图对象,它确实具有
model
属性

handle: (response) =>  # fat arrow here
  @model.set(coords: response)