Skip to content

Instantly share code, notes, and snippets.

@jimkang
Created February 25, 2013 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/5033319 to your computer and use it in GitHub Desktop.
Save jimkang/5033319 to your computer and use it in GitHub Desktop.
copyBoard method for Meteor with problems.
// The callback should take a error object and a boardId.
copyBoard: function(options) {
options = options || {};
if (!(typeof options.boardId === "string" && options.boardId.length)) {
throw new Meteor.Error(400, "Required parameter missing");
}
if (!options.callback) {
throw new Meteor.Error(400, "Required parameter missing");
}
if (! this.userId)
throw new Meteor.Error(403, "You must be logged in");
// Create new board.
var currentBoard = Boards.findOne({ _id: options.boardId });
// Drop the id from the board and insert that as the new board.
delete currentBoard["_id"];
console.log("Inserting board:", currentBoard);
Boards.insert(currentBoard,
function(error, newBoardId) {
if (!error) {
console.log("Inserted id:", newBoardId);
var allBoards = Boards.find().fetch();
// Creates duplicates of the records, giving them have different _ids, then makes
// changes specified in the recordTransformer function to them.
function duplicateRecords(recordArray, collection, recordTransformer) {
// Scrub the _ids out of the records.
var idLessRecords = _.map(recordArray, function(record) {
if ("_id" in record) {
delete record["_id"];
}
return recordTransformer(record);
});
collection.insert(idLessRecords);
}
var userId = this.userId;
function updateBoardAndOwnerInRecord(record) {
record.board = newBoardId;
record.owner = userId;
return record;
};
// Copy associated items and boxes.
duplicateRecords(Items.find({ board: options.boardId }).fetch(), Items,
updateBoardAndOwnerInRecord);
duplicateRecords(Boxes.find({ board: options.boardId }).fetch(), Boxes,
updateBoardAndOwnerInRecord);
options.callback(error, newBoardId);
}
else {
options.callback(error, null);
}
});
}
@jimkang
Copy link
Author

jimkang commented Feb 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment