|
1
|
1 |
(function(){ |
|
|
2 |
'use strict'; |
|
|
3 |
|
|
|
4 |
angular.module('ammicoSearch',['ngResource', 'ngRoute']) |
|
|
5 |
.config(function ($routeProvider) { |
|
|
6 |
$routeProvider |
|
|
7 |
.when('/', { |
|
|
8 |
templateUrl: 'search/search.html', |
|
|
9 |
controller: 'searchCtrl' |
|
|
10 |
}); |
|
|
11 |
}) |
|
|
12 |
.service('searchApi', function($resource, context) { |
|
|
13 |
console.log('search 4',$resource, context); |
|
|
14 |
this.searchResource = function(params){ |
|
|
15 |
return $resource(context.urls.searchUrl, |
|
|
16 |
{ |
|
|
17 |
callback: 'JSON_CALLBACK' |
|
|
18 |
}, |
|
|
19 |
{ |
|
|
20 |
getJsonp: { |
|
|
21 |
method: 'JSONP', |
|
|
22 |
params: params, |
|
|
23 |
isArray: false |
|
|
24 |
} |
|
|
25 |
}); |
|
|
26 |
}; |
|
|
27 |
|
|
|
28 |
}) |
|
|
29 |
.service('searchModel', function(searchApi) { |
|
|
30 |
console.log('search 5'); |
|
|
31 |
this.searchResults = function(params){ |
|
|
32 |
return searchApi.searchResource(params).getJsonp(); |
|
|
33 |
}; |
|
|
34 |
}) |
|
|
35 |
.controller('searchCtrl', function($scope, $location, $routeParams, searchModel){ |
|
|
36 |
console.log('search 6',$scope, $location, $routeParams); |
|
|
37 |
$scope.q = $routeParams.q || ''; |
|
|
38 |
searchModel.searchResults({q:$scope.q, of: 'json', synthesis: 'false', callback: 'JSON_CALLBACK'}).$promise.then( |
|
|
39 |
//success |
|
|
40 |
function( data ){ |
|
|
41 |
console.log('SUCCESS !', data); |
|
|
42 |
$scope.results = data; |
|
|
43 |
}, |
|
|
44 |
//error |
|
|
45 |
function( error ){ |
|
|
46 |
console.log('ERROR !', error); |
|
|
47 |
} |
|
|
48 |
); |
|
|
49 |
//window.myres = res; |
|
|
50 |
//$scope.results = res; |
|
|
51 |
//console.log('search 6-blabla', res); |
|
|
52 |
console.log('search 7', $scope.q); |
|
|
53 |
|
|
|
54 |
//$scope.search = searchModel.search; |
|
|
55 |
// query = ?q=violon&of=json&synthesis=false |
|
|
56 |
}) |
|
|
57 |
.filter('meta', function() { |
|
|
58 |
return function(input, metaName) { |
|
|
59 |
var nb = input.length, i = 0, found = false; |
|
|
60 |
while(found===false && i<nb){ |
|
|
61 |
if(input[i].name===metaName){ |
|
|
62 |
found = true; |
|
|
63 |
return input[i].value; |
|
|
64 |
} |
|
|
65 |
i++; |
|
|
66 |
} |
|
|
67 |
}; |
|
|
68 |
}); |
|
|
69 |
|
|
|
70 |
})(); |