Skip to main content
1 of 2
Viktor
  • 181
  • 5

For me your code looks like very pretty.

Sometimes I write in this manner

Dnianas.Notification = {
    elements: {
        opennotifiiSel: '#opennotifii', //also rename selectors to camelCase, for example
        boxsendnotifSel: '#boxsendnotif',
        boxnotificationsusersSel: '.boxnotificationsusers'
    },

    constants: {
        NOTIFICATION_FADE_OUT: 200
    },

    URL: {
        markAsRead: '/notifications/read',
    },

    init: function() {
        this.bindEvents();
    },

    bindEvents: function() {
        $(document).on('click', this.elements.opennotifiiSel, this.markAsRead.bind(this));
    },

    countNotifications: function() {
        var ids = [];

        // Add unread notifications to the ids array.
        $(this.elements.boxnotificationsusersSel)
            .children() //why we use children() before find()?
            .find(this.elements.boxsendnotifSel)
            .each(function collectboxsendnotifIds() {
                if ($(this).data('read') === 0) {
                    ids.push($(this).data('id'));
                }
            });
        return ids;
    },

    markAsRead: function() {
        var self = this;
        var ids = this.countNotifications();

        if (ids.length > 0) {
            $.ajax({
                url: self.URL.markAsRead,
                data: {
                    notifications: ids,
                    _token: token,
                }
            }).then(function(data) {
                self.renderNotificationCount(data);
            });
        }
    },

    renderNotificationCount: function(data) {
        if (data.seen) {
            $(this.elements.opennotifiiSel + ' .not_nu1').fadeOut(this.constants.NOTIFICATION_FADE_OUT);
        }
    }
};
Viktor
  • 181
  • 5