Spine.js Fetch Custom Records

390 Views Asked by At

I have a spine.js application where I am trying to fetch a list of users who recently commented on the site. My server path is /users/recently_commented.

I tried creating a class method:

Spine.js Video Model

class App.User extends Spine.Model
  @configure 'User', 'name'
  @extend Spine.Model.Ajax

  @recentlyCommented: ->
    $.get @url("recently_commented")

When I pass User.recentlyCommented to my view it returns undefined.

How would I fetch those records?

1

There are 1 best solutions below

0
On BEST ANSWER

I think you have some problems with how you're calling $.get.

First of all, $.get is asynchronous so User.recentlyCommented will return a jqXHR rather than a list of User instances.

Secondly, you should be calling $.get with a callback function of some sort to process the recently_commented data when it arrives:

$.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.

Your view will also need to listen for "new model" created events somehow as the Users won't exist until some time after $.get (and thus recentlyCommented) returns.

Sorry but I don't know Spine so I don't know what the idiomatic approach is here.