Backbone.LocalStorage stores a list of players client-side when calling create
on a players
collection, but the stored models are not being fetched later, even though I can inspect them in localStorage. I can call @collections.players.localStorage.findAll()
to retrieve all the stored objects and manually push them onto my collection.
class App.Models.Player extends Backbone.Model
defaults:
name: 'Unnamed'
team: 'Unassigned'
class App.Collections.Players extends Backbone.Collection
localStorage: new Backbone.LocalStorage('players')
model: App.Models.Player
class App.ViewModels.Game extends kb.ViewModel
constructor: ->
@collections =
players: new App.Collections.Players()
@collections.players.fetch(
success: (collection) =>
console.log(collection) # shows none
console.log(@collections.players.localStorage.findAll())
# shows all the stored players
)
# @players below is rendered by a foreach in a knockout template
@players = kb.collectionObservable @collections.players, { view_model: App.ViewModels.Player }
addPlayer: -> # called when a button is pressed
@collections.players.create(new App.Models.Player({ name: 'New Player' }))
return
Why is Knockback unable to fetch the stored entities automatically?
The following call manually retrieves all objects:
_.each @collections.players.localStorage.findAll(), (elem) =>
@collections.players.add(elem)