Skip to content

Instantly share code, notes, and snippets.

@voleoo
Forked from failpunk/gist:5881482
Created December 11, 2013 22:26

Revisions

  1. @failpunk failpunk created this gist Jun 28, 2013.
    35 changes: 35 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    var cnJqueryUi = angular.module('cn.jquery', []);

    cnJqueryUi.directive('autoComplete', function($http) {

    // by default do not format autocomplete data
    var defaultFormatter = function(data) { return data };

    return {

    scope: {
    source: "@", // autocomplete url to call
    formatter: "=", // (optional) transform data returned before for passing to autocomplete
    callBack: "=", // (optional) callback when user selects an item
    },

    link: function(scope, element, attrs) {

    var formatter = scope.formatter || defaultFormatter
    , onSelect = scope.callBack || {};

    var dataSource = function(term, responseCallBack) {
    $http.get(scope.source + 'term/' + term.term).then(function(response) {
    responseCallBack(formatter(response.data));
    });
    };

    // init jqueryUi autocomplete
    element.autocomplete({
    source: dataSource,
    minLength: 2,
    select: onSelect
    });
    }
    };
    });