Backbone.LocalStorage は、コレクションを呼び出しcreate
たときにプレイヤーのリストをクライアント側にplayers
保存しますが、localStorage でそれらを調べることができても、保存されたモデルは後で取得されません。呼び出し@collections.players.localStorage.findAll()
て、格納されているすべてのオブジェクトを取得し、コレクションに手動でプッシュできます。
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
Knockback が保存されたエンティティを自動的に取得できないのはなぜですか?
次の呼び出しは、すべてのオブジェクトを手動で取得します。
_.each @collections.players.localStorage.findAll(), (elem) =>
@collections.players.add(elem)