Within my view, I am displaying a list of items by utilizing a service that has the following public interface:
return {
items: _items,
getItems: _getItems,
getItemById: _getItemById,
item: _item
};
Here are the local variables within the service:
var _items = [];
var _item;
The 'items' array gets populated when calling 'getItems()'.
When an item is selected from the list, I use 'getItemById(id)' to retrieve and initialize the '_item' variable. However, there seems to be an issue because when trying to access this item from the controller, it's showing as undefined using itemsService.item
.
var _getItemById = function (id) {
_item = null;
$.each(_items, function (i, item) {
if (item.id == id) {
_item = item;
console.log(_item); //this works
return false;
}
});
}
I am looking to track the selected item, especially since I have pages like "ItemsList", "ItemDetail", "ItemExtra" etc. This led me to decide to keep track of the item in my service. Is there a better practice for managing this?