I changed the normal way of using Angular. I'm using my controllers just to talk to my API routes and am using directives to manipulate the data and DOM as well.
I am using the object literal pattern where I have my INIT method that initializes global variables (object) and then calls other methods, bind clicks methods, etc. So, the logic is all the directives.
I'm finding it very confusing and would like the opinion of someone better.
'use strict';
var eventSidebarDirective = {
/**
* Initialize sidebar directive and return the link
* calling all nested methods.
*
*/
init: function() {
return {
link: function(scope) {
scope.$watch('events', function() {
if (scope.events === undefined) {
return;
}
/**
* Every time the user access the event page, this methods
* will be called.
*
*/
EventSidebar.init();
});
},
restrict: 'M'
};
}
};
var EventSidebar = {
init: function() {
this.element = $('[data-event-sidebar]');
this.indicator = $('[data-event-sidebar-to]');
/**
* To manipulate the element, bind a click in the indicator.
*
*/
this.indicator.bind('click', this._toggle.bind(this));
},
_toggle: function(e) {
var state = $(e.currentTarget).data('event-sidebar-to');
this.element
.css({
/**
* If the window width is less than 768px, i just want toggle
* between display block and none.
*
*/
display: 768 >= $(window).width()
? (state
? 'block'
: 'none')
: false
})
.animate({
/**
* But if the window.width is bigger the 768px, i want show
* or hide the sidebar.
*
*/
right: 768 <= $(window).width()
? (state
? '0'
: '-500')
: false
});
}
};
angular
.module('adminApp')
.directive('eventSidebarDirective', eventSidebarDirective.init.bind(eventSidebarDirective));