mirror of
https://github.com/marko-js/marko.git
synced 2026-01-18 14:55:13 +00:00
* refactor out taglib loader/finder/lookup * add comments for taglib apis that we need to deprecate * move components into runtime/core-tags
62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
"use strict";
|
|
/* jshint newcap:false */
|
|
|
|
var BaseState = require("./State");
|
|
var BaseComponent = require("./Component");
|
|
var inherit = require("raptor-util/inherit");
|
|
|
|
module.exports = function defineComponent(def, renderer) {
|
|
if (def.___isComponent) {
|
|
return def;
|
|
}
|
|
|
|
var ComponentClass = function() {};
|
|
var proto;
|
|
|
|
var type = typeof def;
|
|
|
|
if (type == "function") {
|
|
proto = def.prototype;
|
|
} else if (type == "object") {
|
|
proto = def;
|
|
} else {
|
|
throw TypeError();
|
|
}
|
|
|
|
ComponentClass.prototype = proto;
|
|
|
|
// We don't use the constructor provided by the user
|
|
// since we don't invoke their constructor until
|
|
// we have had a chance to do our own initialization.
|
|
// Instead, we store their constructor in the "initComponent"
|
|
// property and that method gets called later inside
|
|
// init-components-browser.js
|
|
function Component(id) {
|
|
BaseComponent.call(this, id);
|
|
}
|
|
|
|
if (!proto.___isComponent) {
|
|
// Inherit from Component if they didn't already
|
|
inherit(ComponentClass, BaseComponent);
|
|
}
|
|
|
|
// The same prototype will be used by our constructor after
|
|
// we he have set up the prototype chain using the inherit function
|
|
proto = Component.prototype = ComponentClass.prototype;
|
|
|
|
// proto.constructor = def.constructor = Component;
|
|
|
|
// Set a flag on the constructor function to make it clear this is
|
|
// a component so that we can short-circuit this work later
|
|
Component.___isComponent = true;
|
|
|
|
function State(component) {
|
|
BaseState.call(this, component);
|
|
}
|
|
inherit(State, BaseState);
|
|
proto.___State = State;
|
|
proto.___renderer = renderer;
|
|
|
|
return Component;
|
|
};
|