Skip to content

Instantly share code, notes, and snippets.

@jking6884
Created June 20, 2016 19:28
Show Gist options
  • Save jking6884/28e21022534990e1ba84a93d33f4453d to your computer and use it in GitHub Desktop.
Save jking6884/28e21022534990e1ba84a93d33f4453d to your computer and use it in GitHub Desktop.
watchTags: Ember.on('init', Ember.observer('tagList', function () {
var tagList = this.get('tagList');
var justTagList = [];
tagList.forEach(function (item) {
justTagList.pushObject(item.get('crmTag'));
});
this.set('selectedTags', justTagList);
this.set('initialTags', justTagList);
})),
actions: {
// create a brand new tag since this wasn't detected in our list
createTag (select, e) {
var self = this;
var store = this.get('targetObject.store');
if (e.keyCode === 13 &&
select.isOpen &&
!select.highlighted &&
!Ember.isBlank(select.searchText)) {
let term = select.searchText;
let isExisting = this.get('tags').findBy('name', term);
// to not call submit
e.preventDefault();
// to avoid calling this twice
e.stopPropagation();
if (!isExisting) {
let newTag = store.createRecord('crm-tag', {name: term});
newTag.save().then(() => {
//register new tag in the widget
let tags = self.get('tags').toArray();
tags.pushObject(newTag);
self.set('tags', tags);
var selectedTags = self.get('selectedTags');
selectedTags.pushObject(newTag);
select.actions.close(e);
}, (reason) => {
self.validationReport(newTag);
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment