1 | //================== MODELS =====================
|
---|
2 | var OAAS_MODEL = (function (my, Backbone) {
|
---|
3 | Backbone.Relational.store.addModelScope(my);
|
---|
4 |
|
---|
5 | my.ControllerModel = Backbone.Model.extend({
|
---|
6 | defaults: {
|
---|
7 | selectedNode: null
|
---|
8 | }
|
---|
9 | });
|
---|
10 |
|
---|
11 | my.Experiment = Backbone.RelationalModel.extend({
|
---|
12 | url: function () {
|
---|
13 | return '/Experiment/Experiment?nodeId=' + this.get('nodeId');
|
---|
14 | },
|
---|
15 | isNew: function () { return false; },
|
---|
16 | defaults: {
|
---|
17 | title: 'My Experiment',
|
---|
18 | isExperiment: true,
|
---|
19 | run: false,
|
---|
20 | nodeId: null,
|
---|
21 | repititions: 1,
|
---|
22 | group: 'TESTAZURE'
|
---|
23 | },
|
---|
24 | relations: [{
|
---|
25 | key: 'experiment',
|
---|
26 | relatedModel: 'ExperimentNode'
|
---|
27 | }]
|
---|
28 | });
|
---|
29 |
|
---|
30 | my.ExperimentNode = Backbone.RelationalModel.extend({
|
---|
31 | defaults: {
|
---|
32 | title: 'Experiment',
|
---|
33 | key: -1,
|
---|
34 | data: {},
|
---|
35 | parent: null,
|
---|
36 | isExperiment: false,
|
---|
37 | nodeId: null
|
---|
38 | },
|
---|
39 | relations: [{
|
---|
40 | type: Backbone.HasMany,
|
---|
41 | key: 'children',
|
---|
42 | relatedModel: 'ExperimentNode',
|
---|
43 | collectionType: 'AlgorithmList',
|
---|
44 | reverseRelation: {
|
---|
45 | key: 'parent'
|
---|
46 | }
|
---|
47 | }],
|
---|
48 | url: function () {
|
---|
49 | return this.get('isExperiment') ? '/Experiment/Experiment?nodeId=' + this.get('nodeId') : '/Experiment/Scenario?nodeId=' + this.get('nodeId');
|
---|
50 | },
|
---|
51 | initialize: function (spec) {
|
---|
52 | if (typeof spec.key === 'undefined' || spec.key == -1) {
|
---|
53 | spec.key = my.ExperimentNode.globalKeyCount;
|
---|
54 | this.set({ key: spec.key });
|
---|
55 | my.ExperimentNode.globalKeyCount++;
|
---|
56 | }
|
---|
57 | else if (spec.key >= my.ExperimentNode.globalKeyCount)
|
---|
58 | my.ExperimentNode.globalKeyCount = spec.key + 1;
|
---|
59 |
|
---|
60 | my.ExperimentNode.globalNodeMap[spec.key] = this;
|
---|
61 | },
|
---|
62 | // adds a new experimentNode to this node
|
---|
63 | addNode: function (experimentNode) {
|
---|
64 | if (!(experimentNode instanceof my.ExperimentNode)) {
|
---|
65 | experimentNode = new my.ExperimentNode(experimentNode);
|
---|
66 | }
|
---|
67 | this.get('children').add(experimentNode);
|
---|
68 | experimentNode.set({ parent: this });
|
---|
69 | this.trigger('node-added', experimentNode);
|
---|
70 | return experimentNode;
|
---|
71 | },
|
---|
72 | // moves an experimentNode into this node
|
---|
73 | moveNode: function (experimentNode) {
|
---|
74 | var parent = experimentNode.get('parent');
|
---|
75 | if (parent != null) {
|
---|
76 | // remove from parent
|
---|
77 | parent.get('children').remove(experimentNode);
|
---|
78 | }
|
---|
79 | this.addNode(experimentNode);
|
---|
80 | },
|
---|
81 | // delete this node
|
---|
82 | remove: function () {
|
---|
83 | var parent = this.get('parent');
|
---|
84 | if (parent != null) {
|
---|
85 | parent.get('children').remove(this);
|
---|
86 | this.set({ parent: null });
|
---|
87 | // simply replace it with 'undefined'
|
---|
88 | delete my.ExperimentNode.globalNodeMap[this.get('key')];
|
---|
89 | }
|
---|
90 | },
|
---|
91 | lookup: function (key) {
|
---|
92 | return my.ExperimentNode.globalNodeMap[key];
|
---|
93 | }
|
---|
94 | },
|
---|
95 | {
|
---|
96 | globalKeyCount: 0,
|
---|
97 | globalNodeMap: {},
|
---|
98 | lookup: function (key) {
|
---|
99 | return my.ExperimentNode.globalNodeMap[key];
|
---|
100 | },
|
---|
101 | nextGlobalKey: function () {
|
---|
102 | var key = my.ExperimentNode.globalKeyCount;
|
---|
103 | my.ExperimentNode.globalKeyCount++;
|
---|
104 | return key;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | );
|
---|
108 |
|
---|
109 | my.ExperimentList = Backbone.Collection.extend({
|
---|
110 | url: function () {
|
---|
111 | return "/Experiment/ExperimentList";
|
---|
112 | },
|
---|
113 | model: my.Experiment
|
---|
114 | });
|
---|
115 |
|
---|
116 | my.AlgorithmList = Backbone.Collection.extend({
|
---|
117 | url: function () {
|
---|
118 | return "/Experiment/AlgorithmList";
|
---|
119 | },
|
---|
120 | model: my.ExperimentNode
|
---|
121 | });
|
---|
122 |
|
---|
123 | my.ScenarioList = Backbone.Collection.extend({
|
---|
124 | url: function () {
|
---|
125 | return "/Experiment/ScenarioList";
|
---|
126 | },
|
---|
127 | model: my.ExperimentNode
|
---|
128 | });
|
---|
129 | return my;
|
---|
130 | } (OAAS_MODEL || {}, Backbone)); |
---|