user3582100 user3582100 - 4 years ago 257
Javascript Question

(Ionic Framework) Angular JS filter factory from another factory?

I have three factory in Items, ItemTags , and Tags. How to create custom search that search Items from another factory. Example when i select tag 'Steak' , the Sirloin is shown in list and when i select tag 'Pizza' the Black Pizza is shown in list. Thanks ...

.factory('Items', function() {
var items = [{
id: 1,
name:'Black Pizza',
main_img:'img/food1.jpg'
}, {
id: 2,
name:'Sirloin',
main_img:'img/food2.jpg'
}];

return {
all: function() {
return items;
},
remove: function(item) {
items.splice(items.indexOf(item), 1);
},
get: function(itemId) {
for (var i = 0; i < items.length; i++) {
if (items[i].id === parseInt(itemId)) {
return items[i];
}
}
return null;
}
};
})

.factory('ItemTags', function(Items,Tags) {
// Might use a resource here that returns a JSON array


var itemTags = [{
id: 1,
itemId:Items.get(1),
tagId:Tags.get(0)
}, {
id: 2,
itemId:Items.get(0),
tagId:Tags.get(1)
}];

return {
all: function() {
return itemTags;
},
remove: function(itemTag) {
itemTags.splice(itemTags.indexOf(itemTag), 1);
},
get: function(itemTagId) {
for (var i = 0; i < itemTags.length; i++) {
if (itemTags[i].id === parseInt(itemTagId)) {
return itemTags[i];
}
}
return null;
}
};
})

.factory('Tags', function() {
var tags = [{
id: 0,
name: 'Steak'
}, {
id: 1,
name: 'Pizza'
}, {
id: 2,
name: 'Delicious'
}];

return {
all: function() {
return tags;
},
remove: function(tag) {
tags.splice(tags.indexOf(tag), 1);
},
get: function(tagId) {
for (var i = 0; i < tags.length; i++) {
if (tags[i].id === parseInt(tagId)) {
return tags[i];
}
}
return null;
}
};
})


I am add this function into ItemTags

getDataByTag: function(tagId , itemId) {
for (var i = 0; i < itemTags.length; i++) {
if (itemTags[i].tagId.id === parseInt(tagId) && itemTags[i].itemId.id===parseInt(itemId)) {
return itemTags[i];
}
}


And this is my filter:

$scope.criteriaMatch = function( tagId) {
return function( item ) {
var itemTag = ItemTags.getDataByTag(tagId,item.id);
if(itemTag!=null)
return true;

return null;
};
};

Answer Source

as per your ques, u did wrong in ItemTags factory.just check and add getDataByTag.

.factory('ItemTags', function(Items,Tags) {
// Might use a resource here that returns a JSON array
Items.get(0);
var itemTags = [{
  id: 1,
  itemId:Items.get(2),
  tagId:Tags.get(0)
}, {
  id: 2,
  itemId:Items.get(1),
  tagId:Tags.get(1)
}];

return {
  all: function() {
    return itemTags;
  },
  remove: function(itemTag) {
    itemTags.splice(itemTags.indexOf(itemTag), 1);
  },
  get: function(itemTagId) {
    for (var i = 0; i < itemTags.length; i++) {
      if (itemTags[i].id === parseInt(itemTagId)) {
        return itemTags[i];
      }
    }
    return null;
  },
  getDataByTag: function(itemTagId) {
    for (var i = 0; i < itemTags.length; i++) {
      if (itemTags[i].tagId.id === parseInt(itemTagId)) {
          return itemTags[i];
      }
    }
    return null;
  }
};
})

u can get ur data like:

.controller('AppCtrl', function($scope,Items,Tags,ItemTags) {
$scope.getData = function(Tagid) {
  console.log("ItemTags :"+JSON.stringify(ItemTags.getDataByTag(Tagid)));
}
  $scope.getData(0);
})

hope,its the same u want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download