Skip to content Skip to sidebar Skip to footer

How To Make Use Of The Data When Calling Fetch On It?

I am using require.js with backbone and backbone.localstorage and I am trying to figure out how to make use of the data after calling fetch, not sure how to go about it... I am try

Solution 1:

You need to fetch and then bind to the reset event on the collection to see when it was successfully pulled the data from the server.

Page = Backbone.View.extend
 el:$('#page')
 render: ->
  songz = new Songs()
  # Initialize view
  songV = new SongV({collection: songz})
  # Render view
  songV.render()
  # Fetch collection
  songz.fetch()

SongV = Backbone.View.extend
  initialize: ->
    @listenTo@collection, "reset", @onResetonReset: (collection) ->
    # Use populated collection data
    ...

Songs = Backbone.Collection.extend
  model: Song
  localStorage: new Backbone.LocalStorage("music")
  initialize: ->

This is how Backbone.LocalStorage stores the collections and models --> here See the table at the bottom, the key for the chain block is you local storage name and then each model has a unique key.

So this means if you have data sitting in local storage that you have put there yourself, you should take it out with a cross-browser local storage device like store.js and then use it to populate your Backbone.Collection.

Alternatively, you could fetch from the server (recommended) and that will populate your collection. Or you could bootstrap the data on page load and reset your collection that way.

Post a Comment for "How To Make Use Of The Data When Calling Fetch On It?"