Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add promise-able search function (fixes #34, #35) #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ A service you can inject in your controller to show the filter bar
The filter object used to filter the items array. The default value is $filter('filter'), however you can also
pass in a custom filter.

- `{function=}` `search`

A custom search functionthat returns a filteredItems array. Return value of this function could be promise, which
is useful for backend API filtering with the text entered in the filter bar.
function (filterText){
var deferred = $q.defer();
$timeout(function(){
// imagine items are return value from you api
var result = items.filter(function(item, index){
return item.text.indexOf(filterText) != -1;
})
deferred.resolve(result);
}, 500);
}

- `{function=}` `expression`

The predicate to be used for selecting items from the `items` array. This is similar to the angular filter
Expand Down
12 changes: 11 additions & 1 deletion demo/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ angular.module('Demo', ['ionic', 'jett.ionic.filter.bar'])
});
})

.controller('MainController', function($scope, $timeout, $ionicFilterBar) {
.controller('MainController', function($scope, $timeout, $q, $ionicFilterBar) {

var filterBarInstance;

Expand All @@ -59,6 +59,16 @@ angular.module('Demo', ['ionic', 'jett.ionic.filter.bar'])
$scope.showFilterBar = function () {
filterBarInstance = $ionicFilterBar.show({
items: $scope.items,
search: function(filterText){
var deferred = $q.defer();
$timeout(function(){
var result = $scope.items.filter(function(item, index){
return item.text.indexOf(filterText) != -1;
})
deferred.resolve(result);
}, 500);
return deferred.promise;
},
update: function (filteredItems, filterText) {
$scope.items = filteredItems;
if (filterText) {
Expand Down
24 changes: 17 additions & 7 deletions dist/ionic.filter.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,13 @@ angular.module('jett.ionic.filter.bar', ['ionic']);
'$compile',
'$timeout',
'$filter',
'$q',
'$ionicPlatform',
'$ionicFilterBarConfig',
'$ionicConfig',
'$ionicModal',
'$ionicScrollDelegate',
function ($document, $rootScope, $compile, $timeout, $filter, $ionicPlatform, $ionicFilterBarConfig, $ionicConfig, $ionicModal, $ionicScrollDelegate) {
function ($document, $rootScope, $compile, $timeout, $filter, $q, $ionicPlatform, $ionicFilterBarConfig, $ionicConfig, $ionicModal, $ionicScrollDelegate) {
var isShown = false;
var $body = $document[0].body;
var templateConfig = {
Expand Down Expand Up @@ -435,6 +436,7 @@ angular.module('jett.ionic.filter.bar', ['ionic']);
cancelText: 'Cancel',
cancelOnStateChange: true,
container: $body,
search: null,
favoritesTitle: 'Favorite Searches',
favoritesAddPlaceholder: 'Add a search term',
favoritesEnabled: false,
Expand Down Expand Up @@ -534,9 +536,11 @@ angular.module('jett.ionic.filter.bar', ['ionic']);
var filterExp, filteredItems;

// pass back original list if filterText is empty.
// Otherwise filter by expression, supplied properties, or filterText.
// Otherwise perform a search or filter by expression, supplied properties, or filterText.
if (!filterText.length) {
filteredItems = scope.items;
} else if (angular.isFunction(scope.search)) {
filteredItems = scope.search(filterText);
} else {
if (scope.expression) {
filterExp = angular.bind(this, scope.expression, filterText);
Expand All @@ -554,11 +558,14 @@ angular.module('jett.ionic.filter.bar', ['ionic']);

filteredItems = scope.filter(scope.items, filterExp, scope.comparator);
}

$timeout(function() {
scope.update(filteredItems, filterText);
scope.scrollItemsTop();
});
// turn filteredItems into a promise for consistent handling logic
$q.when(filteredItems).then(function(items){
$timeout(function() {
scope.update(items, filterText);
scope.scrollItemsTop();
});
})

};

// registerBackButtonAction returns a callback to deregister the action
Expand Down Expand Up @@ -657,6 +664,9 @@ angular.module('jett.ionic.filter.bar', ['ionic']);

})(angular, ionic);




/* global angular */
(function (angular) {
'use strict';
Expand Down
Loading