Sending extra, non-model data to a save request using backbone.js?

I am looking for a solution to solve a state problem between models using backbone.js.

I have a time tracking application in which the user can start / stop tasks, and he will record the time of work. I have a job model that contains work data and whether it is currently 'on'.

Only one task can work at a time. Therefore, if a user starts a task, the current task must be stopped. I am wondering what is the best solution for this? I mean, I could just toggle each 'on' parameter and then call save on each, but this leads to 2 server requests, each of which has a complete view of each job.

Ideally, it would be great if I could copy additional data into a save request in the same way as you can send additional data to a fetch request. I only need to send the identifier of the current work, and since it is really not related to the model, it needs to be sent along with the model, not its part.

Is there a good way to do this? I think I could find a way to maintain a link to the current side of the job server if necessary: ​​\

+8
javascript rest
source share
1 answer

when you call the save function, the first parameter is the data object to be saved. Instead of just calling model.save() , create an object with model data and your additional material.

inside your method that disables saving:

 ... var data = this.model.toJSON(); data.extras = { myParam : someData }; this.model.save(data, {success: function( model, response ) { console.log('hooray it saved: ', model, response); }); ... 
+8
source share

All Articles