Skip to content

Commit

Permalink
Add fillModel to router to allow the trigger of change only for the u…
Browse files Browse the repository at this point in the history
…pdated attribute when performing an update (PUT) of a model, instead of triggering change even for all the attributes.
  • Loading branch information
zbal committed May 23, 2012
1 parent 97b9feb commit a7f2d65
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions servers/Route.bones
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ server.prototype.initializeAssets = function(app) {

server.prototype.initializeModels = function(app) {
this.models = app.models;
_.bindAll(this, 'loadModel', 'getModel', 'saveModel', 'delModel', 'loadCollection');
_.bindAll(this, 'loadModel', 'getModel', 'fillModel', 'saveModel', 'delModel', 'loadCollection');
this.get('/api/:model/:id', this.loadModel, this.getModel);
this.post('/api/:model', this.loadModel, this.saveModel);
this.put('/api/:model/:id', this.loadModel, this.saveModel);
this.put('/api/:model/:id', this.loadModel, this.fillModel, this.saveModel);
this.del('/api/:model/:id', this.loadModel, this.delModel);
this.get('/api/:collection', this.loadCollection.bind(this));
};
Expand Down Expand Up @@ -128,6 +128,19 @@ server.prototype.getModel = function(req, res, next) {
});
};

server.prototype.fillModel = function(req, res, next) {
if (!req.model) return next();
req.model.fetch({
success: function(model, resp) {
next();
},
error: function(model, err) {
err = err instanceof Object ? err.toString() : err;
next(new Error.HTTP(err, 404));
}
});
};

server.prototype.saveModel = function(req, res, next) {
if (!req.model) return next();
req.model.save(req.body, {
Expand Down

0 comments on commit a7f2d65

Please sign in to comment.