Skip to content

Commit

Permalink
Function callback source
Browse files Browse the repository at this point in the history
  • Loading branch information
bcinarli committed Aug 12, 2015
1 parent 11e2de7 commit 5915f28
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/uxrocket.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,23 @@
$el.after('<i class="' + ns.icon + '"></i>');
};

var results = function(request, response) {
console.log("results");
};

var setSource = function($el) {
var _opts = $el.data(ns.data),
fdata = null,
url_pattern = /^(http|https|\/|\.\/|\.\.\/)/,
source = _opts.service,
sourceFunction;

if(typeof source === "function") {
return source;
}


if(!url_pattern.test(source) || typeof source === "object") {
else if(!url_pattern.test(source) || typeof source === "object") {
if(typeof source === "string") {
// we get source from "data-service" attribute,
// so we need to change its format to js array
Expand Down Expand Up @@ -262,6 +270,16 @@
$(this).siblings('.' + ns.loading).removeClass(ns.loading);
});

$el.on('changeData', function(request) {
var term = $(this).data('uiAutocomplete').term,
items = $(this).data('uxAutocomplete').terms[term],
response = $(this).data('uxAutocomplete').response;

response($.map(items.slice(0, _opts.item), function(item) {
return setItem(item);
}));
});

$el.autocomplete({
open : function(event, ui) {
$(".uxitd-autocomplete-category").removeClass('ui-menu-item');
Expand Down
22 changes: 21 additions & 1 deletion tests/sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@
$(".autocomplete").uxitdautocomplete({
service: './autocomplete.php'
});

$("._autocomplete").uxitdautocomplete({
service: function(request, response){
return myfunction(request, response);
}
});
});
</script>

<script>
var dataSource = [{"name":"ActionScript","id":"01", "category":"A"},{"name":"AppleScript","id":"02","category":"A"},{"name":"Asp","id":"03","category":"A"},{"name":"Basic","id":"04","category":"B"},{"name":"Erlang","id":10,"category":"E"},{"name":"Fortran","id":11,"category":"F"},{"name":"Haskell","id":13,"category":"H"},{"name":"Java","id":14,"category":"J"},{"name":"JavaScript","id":15,"category":"J"},{"name":"Scala","id":21,"category":"S"},{"name":"İstanbul","id":15,"category":"İ"}];

var oldVersionDataSource = { "itemList": [{"name":"ActionScript","id":"01", "category":"A"},{"name":"AppleScript","id":"02","category":"A"},{"name":"Asp","id":"03","category":"A"},{"name":"Basic","id":"04","category":"B"},{"name":"Erlang","id":10,"category":"E"},{"name":"Fortran","id":11,"category":"F"},{"name":"Haskell","id":13,"category":"H"},{"name":"Java","id":14,"category":"J"},{"name":"JavaScript","id":15,"category":"J"},{"name":"Scala","id":21,"category":"S"},{"name":"İstanbul","id":15,"category":"İ"}]};

// sample for as
var sample = [{"name":"Asp","id":"03","category":"A"},{"name":"Basic","id":"04","category":"B"},{"name":"Haskell","id":13,"category":"H"},{"name":"JavaScript","id":15,"category":"J"}];

var myfunction = function(request, response){
$("#function-callback").data('uxAutocomplete')["terms"]["as"] = sample;
$("#function-callback").data('uxAutocomplete')["response"] = response;

$("#function-callback").trigger('changeData');
}
</script>

<link href="styles/_uxrocket-jqueryui.css" rel="stylesheet" />
Expand All @@ -38,7 +54,8 @@
width: 350px;
}

.autocomplete:not(.uxitd-plugin-wrap) {
.autocomplete:not(.uxitd-plugin-wrap),
._autocomplete:not(.uxitd-plugin-wrap){
width: 350px;
font-size: 1rem;
padding: 5px;
Expand Down Expand Up @@ -125,6 +142,9 @@
<p class="wrap">
<input type="text" class="autocomplete" id="inline-old" placeholder="Classic view with inline old data source" data-service="oldVersionDataSource" />
</p>
<p class="wrap">
<input type="text" class="_autocomplete" id="function-callback" placeholder="Classic view with service function" />
</p>
</div>
</div>
</body>
Expand Down

0 comments on commit 5915f28

Please sign in to comment.