xoxco / jQuery-Tags-Input

Magically convert a simple text input into a cool tag list with this jQuery plugin.
http://xoxco.com/projects/code/tagsinput/
2.29k stars 829 forks source link

knockoutjs binding not updating #299

Open jlaustill opened 8 years ago

jlaustill commented 8 years ago

I'm thinking this isn't a bug, but more just a lack of compatible binding. I have a simple textarea with a tags id that I use tagsInput on, like

<textarea id="tags" data-bind="value: tags" rows="5"></textarea>

The plugin works fine and adds the tags functionality, but it's not binding to my observable in ko. So if I do

var t = $('#tags').val()

I am getting back the proper delimited string, but

 var t = self.tags();

gives me nothing. I'm sort of new to KnockoutJS, but my understanding is that I would need to write a custom binding to work with tagsInput. I'm wondering if anyone has done this and has an example, or if I'm on my own, or if I'm just totally going about this the wrong way? I'm all ears for any suggestions anyone has whatsoever :)

jlaustill commented 8 years ago

This seems to be doing the trick

ko.bindingHandlers.tagsInput = {
    init: function (element) {
        $ (element).tagsInput ({
            interactive: true,
            delimiter: [","], // Or a string with a single delimiter. Ex: ';'
            removeWithBackspace: false,
            minChars: 0,
            maxChars: 1000,
            width: "100%",
            onChange: function (elem) {
                avm.tags ($ (elem).val ());
    }
        });
    }
};

with this binding

<textarea data-bind="tagsInput: true" rows="4"></textarea>

and this mapping to the viewModel

var avm = new AppViewModel (data);

Not sure if this is the best approach or not.