Skip to content

Instantly share code, notes, and snippets.

@nelix
Created May 20, 2014 03:23
Show Gist options
  • Save nelix/fea59b34443fff7e6222 to your computer and use it in GitHub Desktop.
Save nelix/fea59b34443fff7e6222 to your computer and use it in GitHub Desktop.
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