Created
August 2, 2013 00:02
-
-
Save droyad/6136445 to your computer and use it in GitHub Desktop.
Knockout binding for Tagit
This file contains hidden or 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
| 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]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment