diff --git a/lib/defineRenderer.js b/lib/defineRenderer.js index 3ee4995c8..ec94efc9b 100644 --- a/lib/defineRenderer.js +++ b/lib/defineRenderer.js @@ -43,6 +43,7 @@ function mergeExtendState(widgetState, widgetArgs) { module.exports = function defineRenderer(def) { var template = def.template; + var getInitialProps = def.getInitialProps; var getTemplateData = def.getTemplateData; var getInitialState = def.getInitialState; var getWidgetConfig = def.getWidgetConfig; @@ -60,6 +61,8 @@ module.exports = function defineRenderer(def) { input = {}; } + + if (!loadedTemplate) { loadedTemplate = template.render ? template : marko.load(template); } @@ -86,15 +89,21 @@ module.exports = function defineRenderer(def) { input[k] = global.__rerenderState[k]; } } - widgetState = getInitialState(input); } else { // We are the first widget and we are not being extended // and we are not extending so use the input as the state widgetState = input; + input = null; } - } else { - // We are not the top-level widget so we need to rebuild the state - // from the provided input properties + } + } + + if (!widgetState) { + if (getInitialProps) { + input = getInitialProps(input, out) || {}; + } + + if (getInitialState) { widgetState = getInitialState(input); } } @@ -115,7 +124,7 @@ module.exports = function defineRenderer(def) { } var existingWidget = getExistingWidget(out, widgetArgs); - + if (existingWidget && (existingWidget.constructor === def.constructor) && shouldReuseWidget(existingWidget, widgetState, input)) { // console.log(module.id, 'Reusing existing widget ', existingWidget.id, 'New state: ', widgetState); // Render a placeholder element as a marker that we can use to splice back in the existing diff --git a/test/client/fixtures/components/app-getInitialProps/index.js b/test/client/fixtures/components/app-getInitialProps/index.js new file mode 100644 index 000000000..f50c1c54d --- /dev/null +++ b/test/client/fixtures/components/app-getInitialProps/index.js @@ -0,0 +1,9 @@ +module.exports = require('marko-widgets').defineWidget({ + template: require.resolve('./template.marko'), + getInitialProps: function(input, out) { + var name = input.name; + return { + name: name.toUpperCase() + }; + } +}); \ No newline at end of file diff --git a/test/client/fixtures/components/app-getInitialProps/template.marko b/test/client/fixtures/components/app-getInitialProps/template.marko new file mode 100644 index 000000000..6401101d9 --- /dev/null +++ b/test/client/fixtures/components/app-getInitialProps/template.marko @@ -0,0 +1,3 @@ +