Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .controller('findController', function($scope, ItemFactory, $state, IonicComponent, $window) {
- //navigate to item-view state with object id
- $scope.goToItemView = function(areaId, tagsId, description, name, price, dt, photo, ownerId) {
- ItemFactory.getAreaName(areaId).then(function(res){
- var area = res.data[0].name;
- ItemFactory.getCategoryName(tagsId).then(function(res){
- var tags = res.data[0].name;
- $state.go('item-view', {obj:[area, tags, description, name, price, dt, photo, ownerId]});
- });
- });
- };
- //on page load, load areas, categories, and published items data
- var offers = [];
- ItemFactory.getAreas().then(function(areaData){
- $scope.areas = areaData.data;
- ItemFactory.getCategories().then(function(categoryData){
- $scope.categories = categoryData.data;
- ItemFactory.getAllItems().then(function(itemData){
- var arr = itemData.data;
- for(var i = 0; i < arr.length; i ++) {
- if(arr[i].publish === true) {
- offers.push(arr[i]);
- }
- }
- $scope.items = offers;
- });
- });
- });
- $scope.findItem = function(itemName, item){
- //show loading transition
- $scope.visible = true;
- IonicComponent.Loading.show({template: 'Searching...'});
- $scope.items = {};
- // if user does not enter a name, search by area and category only
- if(angular.equals(itemName, undefined)){
- ItemFactory.getAreaId(item.selectedArea).then(function(res){
- var areaId = res.data[0]._id;
- ItemFactory.getCategoryId(item.selectedCategory).then(function(res){
- var categoryId = res.data[0]._id;
- var query = {
- name: '',
- area: areaId,
- tags: categoryId
- };
- ItemFactory.getItemQuery(query).then(function(res){
- IonicComponent.Loading.hide();
- $scope.search = true;
- $scope.items = res.data;
- });
- });
- });
- }
- // if user does not select an area & category, search by name only
- else if(item === undefined) {
- var query = {
- name: itemName,
- area: '',
- tags: ''
- };
- ItemFactory.getItemQuery(query).then(function(res){
- $scope.items = res.data;
- });
- }
- // if user selects all 3 search options, search by name, area, and category
- else{
- ItemFactory.getAreaId(item.selectedArea).then(function(res){
- var areaId = res.data[0]._id;
- ItemFactory.getCategoryId(item.selectedCategory).then(function(res){
- var categoryId = res.data[0]._id;
- var query = {
- name: itemName,
- area: areaId,
- tags: categoryId
- };
- ItemFactory.getItemQuery(query).then(function(res){
- $scope.items = res.data;
- });
- });
- });
- }
- $scope.load = false;
- };
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement