framework/js/lib/component.js
Toby Zerner e8bb044701 Discussion list refactor, gestures
Also make base Component class automatically assign this.element :)
2015-06-24 17:56:39 +09:30

56 lines
1.0 KiB
JavaScript

/**
*/
export default class Component {
/**
*/
constructor(props) {
this.props = props || {};
this.element = m.prop();
}
/**
*/
$(selector) {
return selector ? $(this.element()).find(selector) : $(this.element());
}
/**
*/
static component(props) {
props = props || {};
if (this.props) {
this.props(props);
}
var view = function(component) {
component.props = props;
var vdom = component.view();
vdom.attrs = vdom.attrs || {};
var oldConfig = vdom.attrs.config;
vdom.attrs.config = function() {
var args = [].slice.apply(arguments);
component.element(args[0]);
if (oldConfig) {
oldConfig.apply(component, args);
}
}
return vdom;
};
view.$original = this.prototype.view;
var output = {
props: props,
component: this,
controller: this.bind(undefined, props),
view: view
};
if (props.key) {
output.attrs = {key: props.key};
}
return output;
}
}