【问题标题】:How to pass session data from collection to view? (Backbone JS / Coffeescript)如何将会话数据从集合传递到视图? (骨干 JS / Coffeescript)
【发布时间】:2015-05-23 01:22:12
【问题描述】:

我有一个包含这两个函数的集合:

class SomeCollection

handleData: (data) ->
  console.log JSON.stringify(data)
  return data

getData: ->
  Session.ajax
      url: "/some/endpoint/"
      type: ‘get'

然后我有视图调用集合中的函数以返回该会话数据:

class SomeView
initialize: =>
  @listenTo @collection

my_session_data = @collection.getData().done @collection.handleData

renderTable: =>
   @$el.html @template
   my_session_data: my_session_data
   console.log JSON.stringify(my_session_data)

根据控制台结果,handleData() 中的 console.log 数据似乎正确显示。但是当从 renderTable() 调用 console.log 时,它会在控制台中返回“未定义”。

如何从视图中获取 renderTable() 以返回在 handleData() 中似乎正常工作/出现的数据?

【问题讨论】:

    标签: javascript ajax backbone.js coffeescript


    【解决方案1】:

    Console.log 函数是异步的,因此您可以同时获得不同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多