mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Fixes #173 - Marko v3: Input data object for custom tags
This commit is contained in:
parent
42b2a349fd
commit
7ded254c8e
@ -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));
|
||||
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1 +0,0 @@
|
||||
Hello Frank! adult=trueHello John! adult=falseHello Jane! adult=true
|
||||
@ -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 }"/>
|
||||
1
test/fixtures/render/autotest/custom-tag-data-arg-with-attrs/expected.html
vendored
Normal file
1
test/fixtures/render/autotest/custom-tag-data-arg-with-attrs/expected.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
Hello Frank! (adult)
|
||||
1
test/fixtures/render/autotest/custom-tag-data-arg-with-attrs/template.marko
vendored
Normal file
1
test/fixtures/render/autotest/custom-tag-data-arg-with-attrs/template.marko
vendored
Normal file
@ -0,0 +1 @@
|
||||
<test-hello({name: 'Frank', adult: false}) adult=true/>
|
||||
1
test/fixtures/render/autotest/custom-tag-data-arg/expected.html
vendored
Normal file
1
test/fixtures/render/autotest/custom-tag-data-arg/expected.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
Hello World!Hello John! (child)
|
||||
2
test/fixtures/render/autotest/custom-tag-data-arg/template.marko
vendored
Normal file
2
test/fixtures/render/autotest/custom-tag-data-arg/template.marko
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
<test-hello({name: 'World'})/>
|
||||
<test-hello({name: 'John', adult: false})/>
|
||||
4
test/fixtures/render/autotest/custom-tag-data-arg/test.js
vendored
Normal file
4
test/fixtures/render/autotest/custom-tag-data-arg/test.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
exports.templateData = {
|
||||
"name": "Frank",
|
||||
"adult": true
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user