Initial commit

This commit is contained in:
Juho Teperi
2013-05-13 11:43:34 +03:00
commit fbf8fb087a
44 changed files with 2425 additions and 0 deletions

20
app/scripts/models/leg.js Normal file
View File

@@ -0,0 +1,20 @@
define([
'backbone'
], function (Backbone) {
var num = 1;
var Leg = Backbone.Model.extend({
initialize: function () {
this.set('airportId', this.get('id'));
this.set('id', num);
++num;
},
destroy: function () {
this.trigger('destroy', this, this.collection, {});
}
});
return Leg;
});

View File

@@ -0,0 +1,45 @@
'use strict';
define([
'backbone',
'backbone-mediator',
'libs/math',
'libs/airports',
'config'
], function (Backbone, BackboneMediator, Math, airports, config) {
var Result = Backbone.Model.extend({
defaults: {
dist: 0,
total: 0,
lto: 0,
co2factor: 0,
ltoCycle: 0,
load: 0,
freight: 0
},
initialize: function (attr, options) {
if (this.get('from') && this.get('to')) {
this.set('dist', Math.haversine(this.get('from'), this.get('to')));
}
this.calculate.apply(this);
},
calculate: function () {
var roundtrip = 0;
if (this.get('from') && this.get('to')) {
var roundtripFactor = (roundtrip) ? 2.0 : 1.0; // fuu
this.set('range', airports.selectRange(this.get('dist')));
this.set(airports.parameters(this.get('from').toJSON(), this.get('to').toJSON(), this.get('range')));
var dist = this.get('dist') * config.indirectRouteMultiplier;
var m = config.radiativeForceFactor(dist) * (1 - this.get('freight')) * (1 / this.get('load'));
this.set('lto', m * this.get('ltoCycle'));
this.set('total', m * this.get('co2factor') * dist + this.get('lto'));
}
}
});
return Result;
});

View File

@@ -0,0 +1,54 @@
'use strict';
define([
'backbone',
'backbone-mediator',
'libs/math',
'libs/airports',
'config'
], function (Backbone, BackboneMediator, Math, airports, config) {
var Result = Backbone.Model.extend({
defaults: {
dist: 0,
total: 0,
rawTotal: 0,
alone: true,
roundtrip: false,
passengers: 1,
price: 0
},
initialize: function () {
Backbone.Mediator.subscribe('roundtrip:change', this.set.bind(this, 'roundtrip'));
Backbone.Mediator.subscribe('passengers:change', this.set.bind(this, 'passengers'));
Backbone.Mediator.subscribe('passengers:change', function (num) {
this.set('alone', num === 1);
}.bind(this));
this.on('change:roundtrip change:passengers', this.calc, this);
},
reset: function () {
this.set('dist', 0);
this.set('total', 0);
this.set('rawTotal', 0);
this.set('price', 0);
},
calc: function () {
var mult = this.get('roundtrip') ? 2 : 1;
this.set('total', mult * this.get('passengers') * this.get('rawTotal'));
this.set('price', this.get('total') / 1000 * config.priceCO2);
},
add: function (another) {
var keys = {
'dist': 'dist',
'total': 'rawTotal'
};
for (var i in keys) {
this.set(keys[i], this.get(keys[i]) + another.get(i));
}
this.calc();
}
});
return Result;
});