components API: take 2
This commit is contained in:
parent
6d01629416
commit
10f291d64a
1 changed files with 26 additions and 13 deletions
39
mithril.js
39
mithril.js
|
|
@ -231,19 +231,13 @@ var m = (function app(window, undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (data != null && dataType === OBJECT) {
|
else if (data != null && dataType === OBJECT) {
|
||||||
var module = m.tags[data.tag], controller = cached.controller
|
if (data.controller) {
|
||||||
if (module) {
|
var module = data
|
||||||
if (!controller) {
|
var controller = cached.controller || new (module.controller || function() {})
|
||||||
var constructor = module.controller || m.prop()
|
if (cached.controller && typeof cached.controller.onupdate === FUNCTION) controller.onupdate.apply(controller, args)
|
||||||
for (var prop in constructor.prototype) data[prop] = constructor.prototype[prop]
|
controller.args = module.controller.$$args
|
||||||
controller = constructor.call(data, data) || data
|
|
||||||
if (!controller.attrs) controller.attrs = {}
|
|
||||||
}
|
|
||||||
controller.tag = data.tag
|
|
||||||
for (var attr in data.attrs) controller.attrs[attr] = data.attrs[attr]
|
|
||||||
controller.children = data.children
|
|
||||||
data = module.view(controller)
|
data = module.view(controller)
|
||||||
if (!data.tag) throw new Error(module.view.toString() + "\n\nThis template must return a virtual element, not an array, string, etc.");
|
if (!data.tag) throw new Error(module.view.toString() + "\n\nThis template must return a virtual element, not an array, string, etc.")
|
||||||
}
|
}
|
||||||
if (!data.attrs) data.attrs = {};
|
if (!data.attrs) data.attrs = {};
|
||||||
if (!cached.attrs) cached.attrs = {};
|
if (!cached.attrs) cached.attrs = {};
|
||||||
|
|
@ -275,7 +269,7 @@ var m = (function app(window, undefined) {
|
||||||
data.children,
|
data.children,
|
||||||
nodes: [node]
|
nodes: [node]
|
||||||
};
|
};
|
||||||
if (module) cached.controller = controller
|
if (controller) cached.controller = controller
|
||||||
|
|
||||||
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
|
if (cached.children && !cached.children.nodes) cached.children.nodes = [];
|
||||||
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
//edge case: setting value on <select> doesn't work before children exist, so set it again after children have been created
|
||||||
|
|
@ -513,8 +507,20 @@ var m = (function app(window, undefined) {
|
||||||
|
|
||||||
var roots = [], modules = [], controllers = [], lastRedrawId = null, lastRedrawCallTime = 0, computePostRedrawHook = null, prevented = false, topModule;
|
var roots = [], modules = [], controllers = [], lastRedrawId = null, lastRedrawCallTime = 0, computePostRedrawHook = null, prevented = false, topModule;
|
||||||
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
var FRAME_BUDGET = 16; //60 frames per second = 1 call per 16 ms
|
||||||
|
function submodule(module, args) {
|
||||||
|
var controller = function() {
|
||||||
|
this.args = args
|
||||||
|
return module.controller.apply(this, args) || this
|
||||||
|
}
|
||||||
|
controller.$$args = args //private, do not use
|
||||||
|
return {
|
||||||
|
controller: controller,
|
||||||
|
view: module.view,
|
||||||
|
}
|
||||||
|
}
|
||||||
m.module = function(root, module) {
|
m.module = function(root, module) {
|
||||||
if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
if (!root) throw new Error("Please ensure the DOM element exists before rendering a template into it.");
|
||||||
|
if (root.controller) return submodule(root, [].slice.call(arguments, 1))
|
||||||
var index = roots.indexOf(root);
|
var index = roots.indexOf(root);
|
||||||
if (index < 0) index = roots.length;
|
if (index < 0) index = roots.length;
|
||||||
var isPrevented = false;
|
var isPrevented = false;
|
||||||
|
|
@ -528,6 +534,7 @@ var m = (function app(window, undefined) {
|
||||||
m.redraw.strategy("all");
|
m.redraw.strategy("all");
|
||||||
m.startComputation();
|
m.startComputation();
|
||||||
roots[index] = root;
|
roots[index] = root;
|
||||||
|
if (arguments.length > 2) module = submodule(module, [].slice.call(arguments, 2))
|
||||||
var currentModule = topModule = module = module || {};
|
var currentModule = topModule = module = module || {};
|
||||||
var constructor = module.controller || m.prop()
|
var constructor = module.controller || m.prop()
|
||||||
var controller = new constructor;
|
var controller = new constructor;
|
||||||
|
|
@ -541,6 +548,12 @@ var m = (function app(window, undefined) {
|
||||||
return controllers[index]
|
return controllers[index]
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
m.module.create = function(module) {
|
||||||
|
var module = submodule(module, [].slice.call(arguments, 1))
|
||||||
|
var controller = new module.controller
|
||||||
|
controller.view = function() {return module.view(controller)}
|
||||||
|
return controller
|
||||||
|
}
|
||||||
m.redraw = function(force) {
|
m.redraw = function(force) {
|
||||||
//lastRedrawId is a positive number if a second redraw is requested before the next animation frame
|
//lastRedrawId is a positive number if a second redraw is requested before the next animation frame
|
||||||
//lastRedrawID is null if it's the first redraw and not an event handler
|
//lastRedrawID is null if it's the first redraw and not an event handler
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue