susi-forge
Version:
86 lines (80 loc) • 2.25 kB
JavaScript
var Controller = function (name, impl) {
this.__name = name;
this.__vars = {};
this.__reupdate = true;
this.__init = false;
this.__started = false;
_.assign(this, impl);
};
Controller.prototype = {
init: function () {
this._init();
return this;
},
_init: function () {
this.__init = true;
},
start: function () {
this._start();
return this;
},
_start: function () {
this.init();
this.__started = true;
this.update();
},
update: function () {
return this._update();
},
_update: function () {
if (this.__reupdate === false) {
console.info(this.__name, 'nothing changed, no update', this);
return true;
}
this.__reupdate = false;
console.info(this.__name, 'updated', this);
return this.render();
},
_render: function (vars) {
this.__vars = vars || this.__vars;
this.__html = this.__template(this.__vars);
this.__rendered = true;
return this.__html;
},
render: function (vars) {
return this._render(vars);
},
renderAt: function (node, vars) {
node.html(this._render(vars));
return this;
},
resolveTemplate: function (name) {
name = name || this.__name;
this.__template =
Context.__templates["src/client/components/" +name+ "/template"] || function () {
console.error('template not found ("src/client/components/' +name+ '/template")');
};
},
data: function (data) {
this.__vars = data || this.__vars;
this.__reupdate = true;
return this;
}
};
var ControllerFactory = {
__instances: {},
create: function (name, impl) {
var tmp = new Controller(name, impl);
tmp.resolveTemplate(name);
this.__instances[name] = tmp;
return this.__instances[name];
},
get: function (name) {
var tmp = this.__instances[name] || undefined;
if (tmp) {
if (tmp.__init === false) tmp.init();
return tmp;
}
console.error(name+" not found, when try to get it with ControllerFactory");
}
};