M
M
microf2015-09-30 08:37:46
Angular
microf, 2015-09-30 08:37:46

How to make a module in Angular.js?

In addition to my previous question
So, there is a service that pulls data with messages, with certain data (read - satus1, not read - status2).

(function () {
    'use strict';

    angular
            .module('app.notifications')
            .factory('notificationService', notificationService);

    notificationService.$inject = ['$resource'];

    function notificationService($resource) {
        return $resource('server/json/notifications.json', {}, {
            getNotificationsData: {method: 'GET', isArray: true}
        });
    }
}());

In the app controller of the app itself, I use it like this
(function () {
    'use strict';
        angular
            .module('app.core')
            .controller('HomeController', HomeController);
//Underscore в зависимости
    HomeController.$inject = ['notificationService', '_'];

    function HomeController(notificationService, _) {

        var vm = this;
        vm.showNotificationList = false;
        vm.unreadNotifications = [];
        vm.readNotifications = [];

        activate();

       // Получаем данные и отправляем их в SplitNotifications (разделяем на прочитанные и нет)
        function activate() {
            return getNotifications().then(function () {
                    SplitNotifications(vm.showNotificationList);
            });
        } ;

            // Получаем данные из сервиса 
    
        function  getNotifications() {
            return notificationService.getNotificationsData()
                    .$promise.then(function (data) {

                        vm.showNotificationList = data;
                        return vm.showNotificationList;
                    });
        }
        ;
       // Разделяем данные, полученные из сервиса на два массива - прочитанных сообщений и не прочитанных
        function SplitNotifications(showNotificationList) {
           
            for (var i = 0; i < showNotificationList.length; i++) {
                vm.unreadNotifications = _.filter(showNotificationList, {status:'1'}); 
                vm.readNotifications = _.filter(showNotificationList, {status:'2'});
                       }
        }
    } ;
})();

But I want to make a separate module and transfer, at a minimum, the data separation logic - SplitNotifications to my module. Where can I hide this? Make the controller of the notifications module, but how will it pass the data to the app? Or also shove it into the notificationService service?
Update In general, I made a separate directive, it contains a controller with a dependency on the service and that's it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question