0

Here is my directive

angular.module('categoryListingDirective', []).directive('categoryListing', function (Category) {
    return {
        restrict: 'A',
        replace: true,
        require: true,
        scope: true,
        templateUrl: '../static/partials/categoryListingForm.html',
        link: function (scope, element, attrs) {
            var categories = undefined;

            var getCategories = function () {
                if (categories === undefined) {
                    categories = Category.query(function(){});
                }
                return categories;
            };

            var allParentCategories = function () {
                console.log('getting parent categories');
                return _.uniq(getCategories(), function (category) {
                    console.log('category:', category);
                    return category.parent;
                });
            };
            console.log('categories:', getCategories());
            console.log('allParentCategories:', allParentCategories());
        }
    }
});

When I try to run this on my browser, I see the following in console log

categories: 
[$promise: Object, $resolved: false]
 categoryListing.js:25
getting parent categories categoryListing.js:19
allParentCategories: [] 

I am pretty sure this should not be empty.

Question

  • Is it because of async nature of calls getting fired?
  • How do I fix it, what are the recommendations?

Thanks

UPDATE

The Category resource looks like

angular.module('categoryService', ['ngResource']).factory('Category', function($resource) {
  return $resource('categories/:categoryId', {categoryId: '@uuid'});
});
2
  • 1
    could you please show your Category resource? At a glance, Category is returning a promise that you need to resolve in your $http success callback. When you call getCategories() you'll be able to call getCategories().then(function(data) { ... }) may want to read about deferred objects ala docs.angularjs.org/api/ng/service/$q Commented May 16, 2014 at 5:40
  • @daydreamer the query call is asynchronous. I think the syntax might be counter-intuitive as pointed out in this question stackoverflow.com/questions/11966252/… Commented May 16, 2014 at 5:46

1 Answer 1

3

(Marked as community wiki since the answer is really in the comments)

This might do the trick and help you to understand the way to handle promises returned by $resource:

var allParentCategories = function () {
    console.log('getting parent categories');
    var ret = [];
    getCategories.then(function(results){
        ret = _.uniq(results.data, function (category) {
            console.log('category:', category);
            return category.parent;
        });
    });
    return ret;
};

I know that it's less pretty than just directly returning the results from _.uniq, but that is life in the current state of JS!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.