From 93eee66ee05073f651e64aa467cbcafb8cceedd4 Mon Sep 17 00:00:00 2001 From: Dylan Piercey Date: Thu, 24 Aug 2017 12:37:20 -0700 Subject: [PATCH] Add ability to update globals by setting new input. --- docs/components.md | 2 +- src/components/Component.js | 3 +++ .../input-global/index.marko | 6 ++++++ .../components-browser/input-global/test.js | 20 +++++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/autotests/components-browser/input-global/index.marko create mode 100644 test/autotests/components-browser/input-global/test.js diff --git a/docs/components.md b/docs/components.md index ed8f9dc03..391a2025f 100644 --- a/docs/components.md +++ b/docs/components.md @@ -483,7 +483,7 @@ this.setStateDirty('numbers'); ### `this.input` -The current input for the component. Setting `this.input` will result in the component being re-rendered. +The current input for the component. Setting `this.input` will result in the component being re-rendered. If a `$global` property is set the `out.global` will also be updated during the re-render, otherwise the existing `$global` is used. ## Variables diff --git a/src/components/Component.js b/src/components/Component.js index ebcb6194c..eddeda780 100644 --- a/src/components/Component.js +++ b/src/components/Component.js @@ -437,6 +437,9 @@ Component.prototype = componentProto = { if (this.___input === undefined) { this.___input = newInput; + if (newInput.$global) { + this.___global = newInput.$global; + } } return newInput; diff --git a/test/autotests/components-browser/input-global/index.marko b/test/autotests/components-browser/input-global/index.marko new file mode 100644 index 000000000..551db5cde --- /dev/null +++ b/test/autotests/components-browser/input-global/index.marko @@ -0,0 +1,6 @@ +class { +} + +
+ Pathname: ${out.global.pathname} +
diff --git a/test/autotests/components-browser/input-global/test.js b/test/autotests/components-browser/input-global/test.js new file mode 100644 index 000000000..752c90815 --- /dev/null +++ b/test/autotests/components-browser/input-global/test.js @@ -0,0 +1,20 @@ +var expect = require('chai').expect; + +module.exports = function(helpers) { + var component = helpers.mount(require('./index.marko'), { + $global: { + pathname:'/' + } + }); + + expect(component.getEl('current').innerHTML).to.equal('Pathname: /'); + + component.input = { + $global: { + pathname: '/test' + } + } + component.update(); + + expect(component.getEl('current').innerHTML).to.equal('Pathname: /test'); +};