Skip to content

Instantly share code, notes, and snippets.

@gaboelnuevo
Forked from droyad/knockout-tags.js
Last active October 12, 2016 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaboelnuevo/1825c3c1d665f0fb0b91031dc042d007 to your computer and use it in GitHub Desktop.
Save gaboelnuevo/1825c3c1d665f0fb0b91031dc042d007 to your computer and use it in GitHub Desktop.
Knockout binding for Tagit
// Based on https://gist.github.com/droyad/6136446
// Use with a observableArray.
// Example:
// <ul data-bind="tags: Tags"></ul>
ko.bindingHandlers.tags = {
init: function(element, valueAccessor, allBindingsAccessor) {
var bind = function() {
var observable = valueAccessor();
observable($(element).tagit("assignedTags"));
};
var options = {
caseSensitive: false,
afterTagAdded: bind,
afterTagRemoved: bind
};
var tags = allBindingsAccessor()["tagsSource"];
if (tags)
$.extend(options, {
autocomplete: {
source: tags,
delay: 0,
minLength: 0
}
});
$(element).tagit(options);
},
update: function(element, valueAccessor) {
var tags = ko.utils.unwrapObservable(valueAccessor());
$(element).tagit("removeAll");
for (var x = 0; x < tags.length; x++) {
$(element).tagit("createTag", tags[x]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment