mirror of
https://github.com/Ekokumppanit/Lentolaskuri.git
synced 2026-02-15 03:47:24 +00:00
Initial commit
This commit is contained in:
53
app/scripts/collections/results.js
Normal file
53
app/scripts/collections/results.js
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
define([
|
||||
'backbone',
|
||||
'models/result',
|
||||
'models/total'
|
||||
], function (Backbone, Result, Total) {
|
||||
var Results = Backbone.Collection.extend({
|
||||
model: Result,
|
||||
initialize: function (model, opts) {
|
||||
this.route = opts.route;
|
||||
|
||||
this.route.on('add', this.newLeg, this);
|
||||
this.route.on('remove', this.calculate, this);
|
||||
this.route.on('sort', this.calculate, this);
|
||||
|
||||
this.total = new Total();
|
||||
},
|
||||
newLeg: function (model, collection, options) {
|
||||
if (this.route.length < 2) {
|
||||
return;
|
||||
}
|
||||
var from;
|
||||
if (this.length > 0) {
|
||||
from = this.route.get(this.last().get('to'));
|
||||
} else {
|
||||
from = this.route.first();
|
||||
}
|
||||
|
||||
var result = new Result({from: from, to: model});
|
||||
this.push(result);
|
||||
this.total.add(result);
|
||||
},
|
||||
calculate: function (model, collection, options) {
|
||||
var models = [];
|
||||
this.total.reset();
|
||||
|
||||
var prev = null;
|
||||
this.route.forEach(function (leg) {
|
||||
if (prev) {
|
||||
var result = new Result({from: prev, to: leg});
|
||||
models.push(result);
|
||||
this.total.add(result);
|
||||
}
|
||||
prev = leg;
|
||||
}.bind(this));
|
||||
|
||||
this.reset(models);
|
||||
}
|
||||
});
|
||||
|
||||
return Results;
|
||||
});
|
||||
19
app/scripts/collections/route.js
Normal file
19
app/scripts/collections/route.js
Normal file
@@ -0,0 +1,19 @@
|
||||
define([
|
||||
'lodash',
|
||||
'backbone',
|
||||
'../models/leg'
|
||||
], function (_, Backbone, Leg) {
|
||||
|
||||
// var stages = [];
|
||||
// var data = {};
|
||||
var num = 1;
|
||||
|
||||
var Route = Backbone.Collection.extend({
|
||||
model: Leg,
|
||||
comparator: function (model) {
|
||||
return model.get('order');
|
||||
}
|
||||
});
|
||||
|
||||
return Route;
|
||||
});
|
||||
Reference in New Issue
Block a user