-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·119 lines (106 loc) · 3.63 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Create the module. Set it up to use html5 mode.
*/
window.MyOpenRecipes = angular.module('myOpenRecipes', ['elasticsearch'],
['$locationProvider', function($locationProvider){
$locationProvider.html5Mode(true);
}]
);
/**
* Create a service to power calls to Elasticsearch. We only need to
* use the _search endpoint.
*/
MyOpenRecipes.factory('recipeService',
['$q', 'esFactory', '$location', function($q, elasticsearch, $location){
var client = elasticsearch({
host: $location.host() + ":9200"
});
/**
* Given a term and an offset, load another round of 10 recipes.
*
* Returns a promise.
*/
var search = function(term, offset){
var deferred = $q.defer();
var query = {
"match": {
"_all": term
}
};
client.search({
"index": 'recipes',
"type": 'recipe',
"body": {
"size": 10,
"from": (offset || 0) * 10,
"query": query
}
}).then(function(result) {
var ii = 0, hits_in, hits_out = [];
hits_in = (result.hits || {}).hits || [];
for(;ii < hits_in.length; ii++){
hits_out.push(hits_in[ii]._source);
}
deferred.resolve(hits_out);
}, deferred.reject);
return deferred.promise;
};
return {
"search": search
};
}]
);
/**
* Create a controller to interact with the UI.
*/
MyOpenRecipes.controller('recipeCtrl',
['recipeService', '$scope', '$location', function(recipes, $scope, $location){
// Provide some nice initial choices
var initChoices = [
"rendang",
"nasi goreng",
"pad thai",
"pizza",
"lasagne",
"ice cream",
"schnitzel",
"hummous"
];
var idx = Math.floor(Math.random() * initChoices.length);
// Initialize the scope defaults.
$scope.recipes = []; // An array of recipe results to display
$scope.page = 0; // A counter to keep track of our current page
$scope.allResults = false; // Whether or not all results have been found.
// And, a random search term to start if none was present on page load.
$scope.searchTerm = $location.search().q || initChoices[idx];
/**
* A fresh search. Reset the scope variables to their defaults, set
* the q query parameter, and load more results.
*/
$scope.search = function(){
$scope.page = 0;
$scope.recipes = [];
$scope.allResults = false;
$location.search({'q': $scope.searchTerm});
$scope.loadMore();
};
/**
* Load the next page of results, incrementing the page counter.
* When query is finished, push results onto $scope.recipes and decide
* whether all results have been returned (i.e. were 10 results returned?)
*/
$scope.loadMore = function(){
recipes.search($scope.searchTerm, $scope.page++).then(function(results){
if(results.length !== 10){
$scope.allResults = true;
}
var ii = 0;
for(;ii < results.length; ii++){
$scope.recipes.push(results[ii]);
}
});
};
// Load results on first run
$scope.loadMore();
}]
);