Skip to content

Instantly share code, notes, and snippets.

@droyad
Created August 2, 2013 00:02
Show Gist options
  • Save droyad/6136446 to your computer and use it in GitHub Desktop.
Save droyad/6136446 to your computer and use it in GitHub Desktop.
Knockout binding for Tagit
ko.bindingHandlers.tags = {
init: function (element, valueAccessor, allBindingsAccessor) {
var bind = function() {
var observable = valueAccessor();
observable($(element).tagit("assignedTags").join(','));
};
var options = {
allowSpaces: true,
caseSensitive: false,
showAutocompleteOnFocus: true,
afterTagAdded: bind,
afterTagRemoved: bind
};
var tags = allBindingsAccessor()["tagsSource"];
if (tags)
$.extend(options, {
autocomplete: { source: tags, delay: 0, minLength: 0 }
});
$(element).tagit(options);
$(element).data('uiTagit').tagInput.css("width", "50px");
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var tags = value.split(',');
$(element).tagit("removeAll");
for (var x = 0; x < tags.length; x++) {
$(element).tagit("createTag", tags[x]);
}
}
}
@gaboelnuevo
Copy link

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