Created
February 25, 2013 21:07
-
-
Save jimkang/5033319 to your computer and use it in GitHub Desktop.
copyBoard method for Meteor with problems.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The resolution for this: https://groups.google.com/d/msg/meteor-talk/gymiMQMTEXQ/HvOsp9XwinoJ