Should you you use classes, prototypes etc in Angular JS? I learned Angular JS before I learned Javascript (bad idea). Now I've started to read some about OOP in javascript. I'm very confused at the moment.
I have an example. It's a service. It has some data from server and some input from user. It runs som logic and populate the object which is then viewed for the user.
Option 1. Just an object. I've removed most of the code so it's more a pseudo code. This is the way you see in most examples and tutorials.
angular.module('calculationServices', ['ngResource']).
factory('CalculationFactory', function(UnitsFactory){
var calculations = {
mainCalculation: function(sizes, pipe, temperature){
if(sizes === null || temperature === null){
return;
}
var rawness = pipe.rawness;
var mediaDensity = temperature.density;
var results = [],
nrOfSizes = sizes.length;
for(var i = 0;i<nrOfSizes;i++){
var flow = this.input.flow.valueInSIunit;
var velocitySiUnit = this.velocityFN(sizes[i].innerdiameter/1000,flow);
var pressureDropInSiUnit = this.pressureDropFN(velocitySiUnit,mediaDensity, frictionFactor, sizes[i].innerdiameter/1000);
results.push({
size:sizes[i].nominalsize
innerDiameter:sizes[i].innerdiameter,
velocity:{
siUnit:velocitySiUnit,
userUnit:velocityUserUnit
}
});
}
return results;
},
powerFN: function(flow, cp, density, deltaT){
var power = flow * cp * deltaT * density;
return power;
},
velocityFN: function(innerDiameter, flow){
var area = Math.PI * Math.pow((innerDiameter) * 0.5 ,2);
return flow / area;
},
input: { //Input is hooked up to my $scope
flow:{
valueInSIunit: 0.00075,
value:0.75,
unit: 'l/s'
}
},
};
return calculations;
})
})
Option 2 Or. should I use classes, instantiating, prototypes etc. Something like this:
factory('CalculationFactory', function(UnitsFactory){
function PipeCalculations(params){
this.flow = params.flow;
this.pipe = params.pipe;
this.sizes = params.sizes;
}
PipeCalculations.prototype.velocityFN = function(flow, innerDiameter){
var area = Math.PI * Math.pow((innerDiameter) * 0.5 ,2);
return flow / area;
}
PipeCalculations.prototype.createPipeCalculationObjects = function(sizes){
var results = [];
var nrOfSizes = sizes.length;
var velocitySiUnit = this.velocityFN(sizes[i].innerdiameter/1000,flow);
for(var i = 0;i<nrOfSizes;i++){
results.push({
size:sizes[i].nominalsize,
velocity:{
siUnit:velocitySiUnit
}
});
}
return results;
};
var pipeCalculations = new PipeCalculations({
flow: { //Should this be hooked up directly to my scope?
valueInSiUnit: 55,
valueInUserUnit: 0.75
},
pipe: 0, //Data feed from controller
sizes: 0
});
pipeCalculations.velocity = pipeCalculations.velocityFN(pipeCalculations.flow.valueInSiUnit, 25);
pipeCalculations.output = pipeCalculations.createPipeCalculationObjects(pipeCalculations.sizes);
return pipeCalculations;
})
Is there a clear right and wrong here? I did option one first. It was very easy and everything worked smoothly. But it didn't feel quite "OOP". However, option 2 is much harder for me because it's difficult to find decent examples. Also, with option one I can simply attach my scope to my model and everything is updated automatically. But how do i do it correctly with option 2? Should i instantiate from my controller?