Bind the tagSource callback to the tag-it context #19

Merged
merged 2 commits into from Aug 31, 2011

5 participants

@GUI

Bind the tagSource callback to the tag-it context. This allows custom callbacks to access tag-it functions, such as assignedTags(), like the default callback does.

Without this change, I couldn't figure out a clean way to access tag-it functions from a custom callback. For example, you could previously do this without problems:

$(".tags").tagit({
  tagSource: function(search, showChoices) {
    var choices = ["a", "b", "c"];
    showChoices(choices);
  }
});

But if you wanted to utilize _subtractArray() and assignedTags() like the default callback does, there didn't seem to be an obvious way, since the custom callback's context is in reference to the jquery autocomplete widget. With this update, you can now do:

$(".tags").tagit({
  tagSource: function(search, showChoices) {
    var choices = ["a", "b", "c"];
    showChoices(this._subtractArray(choices, this.assignedTags()));
  }
});

This obviously changes the context of this callback function, so it's possible this would break existing custom callbacks that were relying on this referencing the autocomplete ui stuff. However, I think the search and showChoices arguments to this function typically give you what you need for autocomplete purposes. But if you can think of a better solution that might work, I'm mainly interested in being able to replicate the default behavior of excluding existing tags based on the results inside a custom tagSource callback that does an ajax call.

GUI added some commits Aug 17, 2011
@GUI GUI Bind the tagSource callback to the tag-it context. This allows custom…
… callbacks to access tag-it functions, such as assignedTags(), like the default callback does.
453e113
@GUI GUI The tagSource option could just be a URL string (as jQueryUI's autoco…
…mplete accepts). Don't bind the callback in that case.
565a6e4
@aehlke aehlke merged commit 00d7a4a into aehlke:master Aug 31, 2011
@aehlke
Owner

thanks!

@baio

Huh. This way you should not only change context of tagSource callback but also all other callbacks, for example onTagAdded. Maby it is more properly to do via
var tagit = $(this).data("tagit") ?

@grisevg

@baio. It should only be done for external methods. onTagAdded will have tag-it object in this, if I'm not mistaken. And it's much cleaner.

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