我有一个spine.js应用程序,我正在尝试获取最近在该网站上发表评论的users的列表。我的服务器路径是/users/recently_commented。
我尝试创建一个类方法:
Spine.js视频模型
class App.User extends Spine.Model
@configure 'User', 'name'
@extend Spine.Model.Ajax
@recentlyCommented: ->
$.get @url("recently_commented")当我将User.recentlyCommented传递给我的视图时,它返回undefined。
我如何获取这些记录?
发布于 2012-05-14 01:09:22
我认为您在调用$.get的方式上有一些问题。
首先,$.get是异步的,因此User.recentlyCommented将返回一个jqXHR,而不是用户实例列表。
其次,您应该使用某种回调函数调用$.get,以便在recently_commented数据到达时对其进行处理:
$.get @url('recently_commented'), (data) ->
# Now convert 'data' to User model instances,
# if this function cares about its context then
# you'll probably want to use => to define it.您的视图还需要以某种方式侦听“新模型”创建的事件,因为用户在$.get (以及recentlyCommented)返回一段时间后才会存在。
对不起,我不知道Spine,所以我不知道这里的惯用方法是什么。
https://stackoverflow.com/questions/10545548
复制相似问题