From 0f2e879d8a0d34bb3ceb302e76e917a42b5995e2 Mon Sep 17 00:00:00 2001 From: cynic89 Date: Sat, 26 Nov 2016 00:24:56 -0500 Subject: [PATCH] Added feature to use a function instead of url to fetch search results. The function must return a promise --- angu_complete_tmpl.html | 15 +++++++++++++++ angucomplete.js | 15 ++++++++++++--- example/angu_complete_tmpl.html | 15 +++++++++++++++ example/index.html | 12 ++++++++++++ example/js/app/controllers/main-controller.js | 11 ++++++++--- 5 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 angu_complete_tmpl.html create mode 100644 example/angu_complete_tmpl.html diff --git a/angu_complete_tmpl.html b/angu_complete_tmpl.html new file mode 100644 index 0000000..4a714f7 --- /dev/null +++ b/angu_complete_tmpl.html @@ -0,0 +1,15 @@ +
+ +
+
Searching...
+
No results found
+
+
+
+
+
+
{{ result.title }}
+
{{result.description}}
+
+
+
diff --git a/angucomplete.js b/angucomplete.js index 734e9a2..90fdc53 100644 --- a/angucomplete.js +++ b/angucomplete.js @@ -23,9 +23,10 @@ angular.module('angucomplete', [] ) "localData": "=localdata", "searchFields": "@searchfields", "minLengthUser": "@minlength", - "matchClass": "@matchclass" + "matchClass": "@matchclass", + "searchFn": "&searchFn" }, - template: '
Searching...
No results found
{{ result.title }}
{{result.description}}
', + templateUrl: 'angu_complete_tmpl.html', link: function($scope, elem, attrs) { $scope.lastSearchTerm = null; @@ -129,6 +130,15 @@ angular.module('angucomplete', [] ) $scope.searching = false; $scope.processResults(matches, str); + } else if($scope.searchFn){ + $scope.$eval($scope.searchFn). + then(function(response) { + $scope.searching = false; + $scope.processResults((($scope.dataField) ? response.data[$scope.dataField] : response.data ), str); + }, function(error) { + console.log("error"); + }); + } else { $http.get($scope.url + str, {}). success(function(responseData, status, headers, config) { @@ -243,4 +253,3 @@ angular.module('angucomplete', [] ) } }; }); - diff --git a/example/angu_complete_tmpl.html b/example/angu_complete_tmpl.html new file mode 100644 index 0000000..4a714f7 --- /dev/null +++ b/example/angu_complete_tmpl.html @@ -0,0 +1,15 @@ +
+ +
+
Searching...
+
No results found
+
+
+
+
+
+
{{ result.title }}
+
{{result.description}}
+
+
+
diff --git a/example/index.html b/example/index.html index d9fdd10..3e40c63 100644 --- a/example/index.html +++ b/example/index.html @@ -38,6 +38,18 @@

Example 2 - People

+ + +
+

Example 3 - Countries of the World with promise. Instead of passing a url, you can pass a function that returns a promise

+
+ +
+
+ You selected {{selectedCountry.originalObject.name}} which has a country code of {{selectedCountry.originalObject.code}} +
+ +
diff --git a/example/js/app/controllers/main-controller.js b/example/js/app/controllers/main-controller.js index 1d3215b..9d7b5ea 100644 --- a/example/js/app/controllers/main-controller.js +++ b/example/js/app/controllers/main-controller.js @@ -1,5 +1,5 @@ -app.controller('MainController', ['$scope', '$http', - function MainController($scope, $http) { +app.controller('MainController', ['$scope', '$http', '$timeout', + function MainController($scope, $http, $timeout) { $scope.people = [ {firstName: "Daryl", surname: "Rowland", twitter: "@darylrowland", pic: "img/daryl.jpeg"}, @@ -253,5 +253,10 @@ app.controller('MainController', ['$scope', '$http', {name: 'Zimbabwe', code: 'ZW'} ]; + $scope.searchFn = function(){ + return $timeout(function(){ + return {data: $scope.countries}; + }, 1000) + }; } -]); \ No newline at end of file +]);