Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. .controller('findController', function($scope, ItemFactory, $state, IonicComponent, $window) {
  2. //navigate to item-view state with object id
  3. $scope.goToItemView = function(areaId, tagsId, description, name, price, dt, photo, ownerId) {
  4. ItemFactory.getAreaName(areaId).then(function(res){
  5. var area = res.data[0].name;
  6. ItemFactory.getCategoryName(tagsId).then(function(res){
  7. var tags = res.data[0].name;
  8. $state.go('item-view', {obj:[area, tags, description, name, price, dt, photo, ownerId]});
  9. });
  10. });
  11. };
  12.  
  13. //on page load, load areas, categories, and published items data
  14. var offers = [];
  15. ItemFactory.getAreas().then(function(areaData){
  16. $scope.areas = areaData.data;
  17. ItemFactory.getCategories().then(function(categoryData){
  18. $scope.categories = categoryData.data;
  19. ItemFactory.getAllItems().then(function(itemData){
  20. var arr = itemData.data;
  21. for(var i = 0; i < arr.length; i ++) {
  22. if(arr[i].publish === true) {
  23. offers.push(arr[i]);
  24. }
  25. }
  26. $scope.items = offers;
  27. });
  28. });
  29. });
  30.  
  31. $scope.findItem = function(itemName, item){
  32. //show loading transition
  33. $scope.visible = true;
  34. IonicComponent.Loading.show({template: 'Searching...'});
  35. $scope.items = {};
  36. // if user does not enter a name, search by area and category only
  37. if(angular.equals(itemName, undefined)){
  38. ItemFactory.getAreaId(item.selectedArea).then(function(res){
  39. var areaId = res.data[0]._id;
  40. ItemFactory.getCategoryId(item.selectedCategory).then(function(res){
  41. var categoryId = res.data[0]._id;
  42. var query = {
  43. name: '',
  44. area: areaId,
  45. tags: categoryId
  46. };
  47. ItemFactory.getItemQuery(query).then(function(res){
  48. IonicComponent.Loading.hide();
  49. $scope.search = true;
  50. $scope.items = res.data;
  51. });
  52. });
  53. });
  54. }
  55. // if user does not select an area & category, search by name only
  56. else if(item === undefined) {
  57. var query = {
  58. name: itemName,
  59. area: '',
  60. tags: ''
  61. };
  62. ItemFactory.getItemQuery(query).then(function(res){
  63. $scope.items = res.data;
  64. });
  65. }
  66. // if user selects all 3 search options, search by name, area, and category
  67. else{
  68. ItemFactory.getAreaId(item.selectedArea).then(function(res){
  69. var areaId = res.data[0]._id;
  70. ItemFactory.getCategoryId(item.selectedCategory).then(function(res){
  71. var categoryId = res.data[0]._id;
  72. var query = {
  73. name: itemName,
  74. area: areaId,
  75. tags: categoryId
  76. };
  77. ItemFactory.getItemQuery(query).then(function(res){
  78. $scope.items = res.data;
  79. });
  80. });
  81. });
  82. }
  83. $scope.load = false;
  84. };
  85.  
  86. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement