When a save, or create is tossed towards the server, the server responds with a new randomly created object. The object can be one of many different Classes, and Backbone responds to these differentiating objects and loads a relative view.
I can only seem to figure this logic out on bootstrap, as no view has been loaded yet, so I can based on what information I am randomly receiving from the server, bootstrap and navigate to that specific route.
However, I am stuck on trying to figure out how to do this when I save an object, and receive my return data.
Here’s my code broken down.
The information is saved.
@model.save(@model.toJSON(),
I have a listenener waiting for this save :
constructor: (options) ->
super(options)
@model.bind 'change:verb', _.chooser, options
_.maestra_chooser is a mixin I have in a utility belt :
_.mixin
_chooser : (item) =>
console.log item
Something to note here. The variable item is unfortunately, the same @model that was just saved. No new data there.
What I’m hoping for item to be is the new variable data from the server, so that I can take that data, see what kind of data it is, and then route to the relevant view.
This is where I believe I’m also making an architecturally unsound idea. But for reasons I don’t understand enough to explain.
Does anyone know where I can access the return data from the server and appropriately navigate my app to that respective route?
Additional Information
This is how I bootstrap it appropriately :
window.router = new Project.Routers.QuestionsRouter(
{
words: #{ @words.to_json.html_safe }
});
Backbone.history.start();
router.navigate("#{@words.kind_of?(Array) ? "bar" : "foo"}", {trigger: true, replace: true})
The change event is only ever going to give you the model and the value that changed…
You can pass a success callback to your save:
where
respwill contain the raw response from the server andmodelwill contain the server side state of your model.You can also bind to your model’s sync event as mentioned in the comments:
the
synccallback is called with arguments: model, resp and options where options is the set of options passed tosave.https://github.com/documentcloud/backbone/blob/9a12b7640f07839134e979b66df658b70e6e4fe9/backbone.js#L383
Not really sure why you are expecting to get data back from a save that’ll change your page though. Seems a bit odd.
What type of data are you expecting to receive after a save that wouldn’t be in your model?