I may be missing something simple here but I can't seem to find the answer I'm looking for.
I'm writing the front end for a cart in CoffeeScript using Backbone and while the server REST api is being completed I'm trying to use the localStorage adapter.
Basically in my application entry I want to grab the cart if it exists, otherwise create a new one.
I know I can grab the JSON from the localStorage but how can I fetch it as a Cart model on page load?
I basically want something like this (with the cart.get(1) being what I need)
# entry point
$ ->
cart = cart.get(1) || new Cart123.Cart id: 1
new Cart123.CheckoutView model: cart
# model
class Cart123.Cart extends Backbone.Model
localStorage: new Store 'Cart'
defaults: ->
State: new Cart123.State
Instances: new Cart123.Instances [ new Cart123.Instance ]
I'm assuming I don't need a cart collection because there's always only going to be one cart but as far as I know a collection is the only way to grab a model by ID.
Thanks for any pointers
If you've got the JSON for the model, the only thing you have to do is instantiate the Backbone model and pass the JSON. The JSON will be vivified into an actual model representing the data:
cart = new Cart800.Cart(cart.get(1) || id: 1)
new Cart800.CheckoutView model: cart