I am working on a small application with a model named JobStatuses in Backbone/Express that needs to be synchronized occasionally. According to the Backbone documentation:
save method allows success and error callbacks in the options hash, which will receive the arguments (model, response, options). In case of server-side validation failure, it should return a non-200 HTTP response code along with an error response in text or JSON format.
However, even though the server returns a status code of 200, my success/failure callbacks are not being triggered when I call JobStatuses.save. I have provided the code for my view, model, and route with the server-side handler function. The updateJobs function is the one that is being called. Can someone explain why the JobStatuses.save callbacks are not being triggered?
Below is the view responsible for triggering JobStatuses.save. I can confirm that the render function is called successfully here.
reqs = ['jquery', 'backbone', 'underscore', '../models/jobstatuses']
define(reqs, ($, Backbone, _, JobStatuses)->
class ButtonView extends Backbone.View
el: $('.button-container'),
template: _.template($('#button-template').html())
constructor: ()->
this.listenTo(JobStatuses, 'sync', this.render)
this.listenTo(JobStatuses, 'save', this.render)
JobStatuses.fetch()
render: ()->
this.el.html(this.template(jobStatuses: JobStatuses.attributes))
this.buttons = this.el.find('.job-button')
this.buttons.click(()->
category = $(this).attr('category')
job = $(this).attr('job')
JobStatuses.attributes[category][job] = 'Completed'
JobStatuses.save(
success: (() ->
console.log('statuses')
console.log(JobStatuses.attributes)),
error: (()->
console.log('failure'))
)
)
)
The JobStatuses model:
define(['backbone'], (Backbone)->
class JobStatuses extends Backbone.Model
url: '/jobs'
new JobStatuses
)
My route. Only the updateJobs function is called:
/*
* GET home page.
*/
var RUNNING = 'Running';
var COMPLETED = 'Completed';
var BLOCKED = 'Blocked';
var NOT_STARTED = 'Not started';
var jobs = {
'oon': {
'oonload': NOT_STARTED,
'oonstagecopy': NOT_STARTED
},
'icd': {
'icdload': NOT_STARTED,
'icdstagecopy': NOT_STARTED
}
}
exports.index = function(req, res){
res.render('index', { title: 'Backbonejs Batchstart' });
};
exports.getJobs = function(req, res){
//res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(jobs));
};
exports.updateJobs = function(req, res){
jobs = req.body;
console.log(req.body);
console.log(jobs);
res.send(JSON.stringify(jobs));
};