我将spine.js 与Spine.Ajax
模块结合使用,通过JSON 从服务器加载内容。我可能遇到了一些同步问题。我有一个侧边栏,它仅绑定(bind)到刷新和更改事件,然后呈现:
Survey.bind 'refresh change', @render
我还设置了一些路线,当用户通过 #/survey/:id 访问它时,这些路线会显示调查。这是我的 Controller :
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
Controller (由名为 App.SurveyStack
的 Spine.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)
我不断从源代码的注释部分收到错误:未捕获的未知记录
。我可以阻止 Survey.find()
函数直到 Survey.fetch()
完成吗?
最佳答案
您的诊断是正确的,您在从服务器获取项目之前无法找到该项目。
最简单的解决方案是将事件绑定(bind)到 Survey.fetch() 调用,如下所示:
Survey.bind 'refresh change', @method_to_call
如果有帮助请告诉我!
关于ajax - 脊柱.js: "Uncaught Unknown record",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8980925/