【问题标题】:spine.js: "Uncaught Unknown record"spin.js:“未捕获的未知记录”
【发布时间】:2012-01-24 01:14:22
【问题描述】:

我将spine.js 与Spine.Ajax 模块结合使用,通过JSON 从服务器加载内容。我可能遇到了一些同步问题。我有一个侧边栏,它只绑定到刷新和更改事件,然后被渲染:

Survey.bind 'refresh change', @render

我还设置了一些路由,当用户通过#/survey/:id 访问它时会显示一个调查。这是我的控制器:

class App.Surveys extends Spine.Controller
  className: 'surveys'

  constructor: ->
    super

    @append(@sidebar = new App.Sidebar)       # Sidebar to select surveys
    @append(@surveys = new App.SurveysStack)  # Show survey details survey

    @routes
     '/surveys/:id': (params) ->
       @sidebar.active(params)
       @surveys.show.active(params)

    Survey.fetch()

如您所见,Survey.fetch() 在初始化之后被调用,这不会对侧边栏造成问题。但是,它似乎给调查Show 控制器(由称为App.SurveyStackSpine.Stack 调用)带来了问题:

class Show extends Spine.Controller
  constructor: ->
    super
    @active @change

  change: (params) =>
    # There is a bug! If Survey is not fetched when we run this,
    # this throws an error.
    @item = Survey.find(params.id)
    @render()

  render: ->
    @html @view("surveys/show")(@item)

我不断收到来自源注释部分的错误:Uncaught Unknown record。我可以在完成Survey.fetch() 之前制作Survey.find() 功能块吗?

【问题讨论】:

    标签: ajax coffeescript spine.js


    【解决方案1】:

    您的诊断是正确的,您在从服务器获取项目之前无法找到它。

    最简单的解决方案是将事件绑定到 Survey.fetch() 调用,如下所示:

    Survey.bind 'refresh change', @method_to_call
    

    如果有帮助,请告诉我!

    【讨论】:

      【解决方案2】:

      实际上对我有用的解决方案是在对象刷新后初始化 Spine.Route.setup()。而不是 app/index.js.coffee 中的标准调用,它将是:

      App.Survey.one 'refresh', ->
        Spine.Route.setup()
      

      【讨论】:

      • 你有没有发现为什么 Spine 会这样?我有一个类似的问题 - 巧合的是在一个调查应用程序中:P
      猜你喜欢
      • 2021-07-25
      • 2010-11-08
      • 2021-10-05
      • 1970-01-01
      • 2014-11-26
      • 2011-09-08
      • 2012-01-06
      • 1970-01-01
      • 2015-03-01
      相关资源
      最近更新 更多