mattrc
1/14/2015 - 3:40 AM

hsEntitiesSuggestedList

hsEntitiesSuggestedList

'use strict';

app.directive('hsEntitiesSuggestedList', function (FollowBtnService) {

    // Helper fn to find DOM element by scope ID
    function _getElementByScopeId(domCollection, id) {
        return _.find(domCollection, function (domElement) {
            return (angular.element(domElement).scope().entity.id === id);
        });
    }

    return {
        restrict: 'E',
        scope: {
            'entities': '='
        },
        templateUrl:'/scripts/directives/entitiesSuggestedList/entitiesSuggestedList.html',
        link: function (scope, element) {
            FollowBtnService.onFollowingChanged(function (entity, action) {

                if (entity && action === 'follow') {

                    var domElement = _getElementByScopeId(element.find('.ng-scope'), entity.id);

                    angular.element(domElement).fadeOut(400, function () {
                        _.remove(scope.entities, function (e) {
                            return e.id === entity.id;
                        });
                    });

                }

            }, scope);
        }
    };

});