Skip to content

Commit

Permalink
Some more progress on blueprints - relies on WL fix for 'populate' ac…
Browse files Browse the repository at this point in the history
…tion to work with an explicit child id
mikermcneil committed Mar 20, 2014

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ec7e064 commit e2e25d6
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/hooks/blueprints/actions/add.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Add a member to an association
*
* @param {Integer|String} parentid - the unique id of the parent record
* @param {Integer|String} id - the unique id of the child record to add
* @param {Integer|String} id - the unique id of the parent record
* @param {Integer|String} childid - the unique id of the child record to add
*
* @option {String} model - the identity of the model
* @option {String} alias - the name of the association attribute (aka "alias")
@@ -20,17 +20,17 @@ module.exports = function add(req, res) {
var Model = sails.models[model];

// Locate and validate required parameters
var parentid = req.param('parentid');
if (!parentid) {
return res.badRequest('No parentid provided.');
}
var id = req.param('id');
if (!id) {
return res.badRequest('No id provided (primary key of the record to be added.)');
return res.badRequest('No `id` provided.');
}
var childid = req.param('childid');
if (!childid) {
return res.badRequest('No `childid` provided (primary key of the record to be added.)');
}

Model
.findOne(parentid)
.findOne(id)
.exec({
error: res.serverError,
success: function found(matchingRecord) {
@@ -40,7 +40,7 @@ module.exports = function add(req, res) {
var associated = matchingRecord[alias];

try {
associated.add(id);
associated.add(childid);
}
catch (err) {
// Ignore `insert` errors
@@ -68,12 +68,12 @@ module.exports = function add(req, res) {
}

// Publish to subscribed sockets
Model.publishAdd(matchingRecord[Model.primaryKey], alias, id, !sails.config.blueprints.mirror && req);
Model.publishAdd(matchingRecord[Model.primaryKey], alias, childid, !req.options.mirror && req);

}

Model
.findOne(parentid)
.findOne(childid)
.populate(alias)
.exec({
error: res.serverError,

0 comments on commit e2e25d6

Please sign in to comment.