2018-08-04 21:46:25 +00:00
|
|
|
"use strict";
|
|
|
|
var counter = 0;
|
|
|
|
var Subscribable = require("../utils/subscribable");
|
2018-12-02 15:07:43 +00:00
|
|
|
var Controller = require("./controller");
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
var LocalModel = Subscribable.inherit({
|
|
|
|
"className": "LocalModel",
|
2018-10-28 21:32:44 +00:00
|
|
|
"constructor": function(properties, data) {
|
2018-08-04 21:46:25 +00:00
|
|
|
Subscribable.fn.constructor.call(this);
|
|
|
|
|
|
|
|
this.properties = [];
|
|
|
|
this._controllers = [];
|
2018-12-02 15:07:43 +00:00
|
|
|
this._foreignControllers = [];
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
if (properties) {
|
|
|
|
for (var key in properties) {
|
|
|
|
if (properties.hasOwnProperty(key)) {
|
|
|
|
var pair = {p: key, k: properties[key]};
|
|
|
|
this.properties.push(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 21:32:44 +00:00
|
|
|
|
|
|
|
if (data !== undefined) {
|
|
|
|
this.data = data;
|
|
|
|
this.initialized = true;
|
|
|
|
}
|
2018-08-04 21:46:25 +00:00
|
|
|
},
|
2018-11-28 06:54:14 +00:00
|
|
|
"destructor": function() {
|
2018-12-02 15:07:43 +00:00
|
|
|
for (i = 0; i < this._foreignControllers.length; ++i) {
|
|
|
|
var pair = this._foreignControllers[i];
|
|
|
|
global.unsubscribeForeignController(pair.n, pair.c);
|
2019-01-03 00:26:42 +00:00
|
|
|
global.unregisterForeignController(pair.n, pair.c);
|
2018-12-02 15:07:43 +00:00
|
|
|
|
|
|
|
pair.c.destructor();
|
|
|
|
}
|
|
|
|
|
2018-11-28 06:54:14 +00:00
|
|
|
for (var i = 0; i < this._controllers.length; ++i) {
|
|
|
|
this._controllers[i].destructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
Subscribable.fn.destructor.call(this);
|
|
|
|
},
|
|
|
|
"addController": function(ctrl) {
|
|
|
|
this._controllers.push(ctrl);
|
|
|
|
},
|
2018-12-02 15:07:43 +00:00
|
|
|
"addForeignController": function(nodeName, ctrl) {
|
|
|
|
if (!(ctrl instanceof Controller)) {
|
|
|
|
throw new Error("An attempt to add not a controller into " + this.className);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._foreignControllers.push({n: nodeName, c: ctrl});
|
|
|
|
ctrl.on("serviceMessage", this._onControllerServiceMessage, this);
|
|
|
|
|
|
|
|
global.registerForeignController(nodeName, ctrl);
|
|
|
|
global.subscribeForeignController(nodeName, ctrl);
|
|
|
|
},
|
|
|
|
"_onControllerServiceMessage": Controller.fn._onControllerServiceMessage,
|
|
|
|
"removeForeignController": function(ctrl) {
|
|
|
|
if (!(ctrl instanceof Controller)) {
|
|
|
|
throw new Error("An attempt to remove not a controller from " + this.className);
|
|
|
|
}
|
|
|
|
for (var i = 0; i < this._foreignControllers.length; ++i) {
|
|
|
|
if (this._foreignControllers[i].c === ctrl) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i === this._foreignControllers.length) {
|
|
|
|
throw new Error("An attempt to remove not not existing controller from " + this.className);
|
|
|
|
} else {
|
|
|
|
var pair = this._foreignControllers[i];
|
|
|
|
global.unsubscribeForeignController(pair.n, pair.c);
|
|
|
|
global.registerForeignController(pair.n, pair.c);
|
|
|
|
|
|
|
|
pair.c.off("serviceMessage", this._onControllerServiceMessage, this);
|
|
|
|
|
|
|
|
this._foreignControllers.splice(i, 1);
|
|
|
|
}
|
|
|
|
},
|
2018-08-04 21:46:25 +00:00
|
|
|
"setData": function(data) {
|
|
|
|
this.data = data;
|
2018-08-29 19:54:18 +00:00
|
|
|
this.initialized = true;
|
2018-08-04 21:46:25 +00:00
|
|
|
this.trigger("data");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = LocalModel;
|