I am a newcomer to javascript and AngularJS.
So... Maybe it's a simple question, but I've noticed two ways to define functions in javascript.
In the following controllers, pay attention to "grupoCancha" and "grupoVisible" (I included the entire script because there are other variables that need to be defined depending on the function).
Controller:
(function() {
'use strict';
angular
.module('example.cancha')
.controller('CanchaController', CanchaController);
CanchaController.$inject = ['$state', 'canchaService'];
function CanchaController($state, canchaService) {
var vm = angular.extend(this, {
canchasComplejo: [],
grupoCancha: grupoCancha,
grupoVisible: grupoVisible
});
(function activate() {
loadCanchasComplejo();
})();
//function that calls the service to get the complex fields
function loadCanchasComplejo() {
canchaService.obtainComplexFields()
.then(function(canchasComplejo) {
vm.canchasComplejo = canchasComplejo;
});
}
function grupoCancha(canchaComplejo){
if (vm.grupoVisible(canchaComplejo)) {
vm.grupoShown = null;
}
else {
vm.grupoShown = canchaComplejo;
}
}
function grupoVisible(canchaComplejo){
return vm.grupoShown === canchaComplejo;
}
}
})();
The other way is a bit odd (maybe because of my background in Java). But the function definition is quite complex:
Controller 2:
(function() {
'use strict';
angular
.module('example.cancha')
.controller('CanchaController', CanchaController);
CanchaController.$inject = ['$state', 'canchaService'];
function CanchaController($state, canchaService) {
var vm = angular.extend(this, {
canchasComplejo: []
});
(function activate() {
loadCanchasComplejo();
})();
//function that calls the service to get the complex fields
function loadCanchasComplejo() {
canchaService.obtainComplexFields()
.then(function(canchasComplejo) {
vm.canchasComplejo = canchasComplejo;
});
}
vm.grupoCancha = function(canchaComplejo) {
if (vm.grupoVisible(canchaComplejo)) {
vm.grupoShown = null;
}
else {
vm.grupoShown = canchaComplejo;
}
};
vm.grupoVisible = function(canchaComplejo) {
return vm.grupoShown === canchaComplejo;
};
}
})();
Could someone please explain which method is better for defining functions and why? Thank you!!