Fixes #173 - Marko v3: Input data object for custom tags

This commit is contained in:
Patrick Steele-Idem 2016-01-19 15:34:52 -07:00
parent 42b2a349fd
commit 7ded254c8e
10 changed files with 47 additions and 21 deletions

View File

@ -155,6 +155,29 @@ class CustomTag extends HtmlElement {
}
}
// Store the renderBody function with the input, but only if the body does not have
// nested tags
if (renderBodyFunction && !hasNestedTags) {
inputProps.renderBody = renderBodyFunction;
}
inputProps = builder.literal(inputProps);
var argument = this.argument;
if (argument) {
argument = builder.parseExpression(argument);
if (Object.keys(inputProps.value).length === 0) {
inputProps = argument;
} else {
var mergeVar = codegen.addStaticVar('__merge', '__helpers.m');
inputProps = builder.functionCall(mergeVar, [
inputProps, // Input props from the attributes take precedence
argument
]);
}
}
var rendererPath = tagDef.renderer;
var rendererRequirePath;
@ -168,24 +191,13 @@ class CustomTag extends HtmlElement {
}
if (tagDef.template) {
if (renderBodyFunction) { // Store the renderBody function with the input
inputProps.renderBody = renderBodyFunction;
}
let templateRequirePath = context.getRequirePath(tagDef.template);
let templateVar = context.importTemplate(templateRequirePath);
let renderMethod = builder.memberExpression(templateVar, builder.identifier('render'));
let renderArgs = [ builder.literal(inputProps), 'out' ];
let renderArgs = [ inputProps, 'out' ];
let renderFunctionCall = builder.functionCall(renderMethod, renderArgs);
return renderFunctionCall;
} else {
// Store the renderBody function with the input, but only if the body does not have
// nested tags
if (renderBodyFunction && !hasNestedTags) {
inputProps.renderBody = renderBodyFunction;
}
var loadTagVar = codegen.addStaticVar('__loadTag', '__helpers.t');
var loadTagArgs = [
@ -209,7 +221,7 @@ class CustomTag extends HtmlElement {
var loadTag = builder.functionCall(loadTagVar, loadTagArgs);
let tagVar = codegen.addStaticVar(tagDef.name, loadTag);
let tagArgs = [ builder.literal(inputProps), 'out' ];
let tagArgs = [inputProps, 'out' ];
if (isNestedTag || hasNestedTags) {
tagArgs.push(isNestedTag ? parentTagVar : builder.literal(0));

View File

@ -18,7 +18,6 @@
var escapeXml = require('raptor-util/escapeXml');
var escapeXmlAttr = escapeXml.attr;
var runtime = require('./'); // Circular dependency, but that is okay
var extend = require('raptor-util/extend');
var attr = require('raptor-util/attr');
var attrs = require('raptor-util/attrs');
var isArray = Array.isArray;
@ -291,9 +290,19 @@ module.exports = {
return this;
},
/**
* Internal helper method to do a shallow copy of the properties of one object to another.
* @private
* Merges
* @param {[type]} object [description]
* @param {[type]} source [description]
* @return {[type]} [description]
*/
xt: extend
m: function(into, source) {
for (var k in source) {
if (source.hasOwnProperty(k) && !into.hasOwnProperty(k)) {
into[k] = source[k];
}
}
return into;
}
};

View File

@ -1 +0,0 @@
Hello Frank! adult=trueHello John! adult=falseHello Jane! adult=true

View File

@ -1,3 +0,0 @@
<test-simpleHello c-input="data"/>
<test-simpleHello c-input="{ name: 'John', adult: false }"/>
<test-simpleHello c-data="{ name: 'Jane', adult: true }"/>

View File

@ -0,0 +1 @@
Hello Frank! (adult)

View File

@ -0,0 +1 @@
<test-hello({name: 'Frank', adult: false}) adult=true/>

View File

@ -0,0 +1 @@
Hello World!Hello John! (child)

View File

@ -0,0 +1,2 @@
<test-hello({name: 'World'})/>
<test-hello({name: 'John', adult: false})/>

View File

@ -0,0 +1,4 @@
exports.templateData = {
"name": "Frank",
"adult": true
};