Search code examples
ajaxcoffeescriptspine.js

How to react properly on fetched data in spinejs


I've set up a small spinejs application with an api as backend for storing new data und serving already saved data. When my app is loading I'm doing the following:

require('lib/setup')

Spine = require('spine')
Posts = require('controllers/posts')
Post = require('models/post')

class App extends Spine.Controller
  constructor: ->
  super

  @log("App up and running :)")

  @posts = new Posts
  Post.fetch()

  #defining starting point of application
  @routes
    '/': ->
       @posts.main.show_all.active()

  Spine.Route.setup()

  @html @posts.active()

module.exports = App

My target is to display the latest 5 posts when visiting the index page. Fetching the data works fine but I'm only able to see the data when I revisit the index page via a ink. What must I do to see the posts just after they have been loaded!? I've already red something that I have to react on the refresh event. But right now I don't know where I should add the listener and what it must do. Home someone can give me a hint.


Solution

  • you have to bind a handler to the refresh event for your Post model

    Post.fetch()
    Post.bind 'refresh', @onPostsFetched
    

    where onPostsFetched is a method of your controller

    onPostsFetched: ->
      @posts = Post.all()
    

    hope this helps