Reputation: 4021
I'm using backbone.js to structure my client side app. Backbone.sync is working nicely for all the CRUD operations, however I'm unclear how to implement one of the server side requests I need to make. This request involves sending a model containing algorithm parameters to the server, and receiving a response containing the results of the algorithm (chart data points, tabular data etc). Calling model.save() doesn't feel like the right thing to do, because backbone is expecting a response containing the updated model, whereas I would ideally like to create an entirely new model with the response.
Should I simply fall back to jQuery.ajax({data:model.toJson(),...})
and create a new model with the response? Or is there something else clever that I'm missing?
Many thanks.
Upvotes: 2
Views: 389
Reputation: 38782
Although I agree with the @mu's answer, I want to say that always is a way to encapsulate a non-CRUD operation in a CRUD one.
For example in your case you can have a Model with loaded attributes those are sent to the server as the paramaters needed for create the instance.
The server is expecting a POST action to create an entity of, let's say, Chart
Model. The server take the params to build the Chart, and returns the JSON representation of the Chart including both: the initial params and the result params.
Then, in the Backbone layer, the Model will contain all the data you need, not only the result data but also the original data.
Upvotes: 2
Reputation: 434665
What you want to do doesn't sound like Create, Update, Read, or Destroy, it sounds more like an RPC call. There's no reason to try to shoehorn a non-CRUD operation into a CRUD system like Backbone.sync
, you're free to call $.ajax
yourself whenever it makes sense and an RPC-style call sounds like a good use for doing some manual AJAXing. Presumably you'd convert the success
results into one or more set
calls and let Backbone's event system take it from there.
Upvotes: 8