Created
May 20, 2014 03:23
-
-
Save nelix/fea59b34443fff7e6222 to your computer and use it in GitHub Desktop.
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
handleTagChange: function(tags) { | |
var tagCollection = this.props.tagCollection; | |
var taskModel = this.props.model; | |
function storeTags() { | |
if (taskModel.isNew()) { | |
taskModel.set({tag_ids: ids}); | |
} else { | |
taskModel.save({tag_ids: ids}, {patch: true}); | |
} | |
} | |
var allTags = tagCollection.pluck('name'); | |
var newTags = _.difference(tags, allTags); | |
var newTagModels = newTags.map( | |
function createTagModel(tagName) { | |
return new Tag.Model({name: tagName}); | |
} | |
); | |
var tagIds = tags.map( | |
function findExistingTags(tag) { | |
return tagCollection.where({name: tag}); | |
} | |
); | |
if (newTagModels) { | |
var complete = _.invoke(tagCollection.add(newTagModels), 'save'); | |
$.when.apply($, complete).done( | |
function finishCreatingTags(){ | |
var newTagIds = _.pluck(newTagModels, 'id'); | |
storeTags(_.union(tagIds, newTagIds)); | |
} | |
); | |
} else { | |
storeTags(tagIds); | |
} | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment