Skip to content

Commit

Permalink
fix(component): access globals and optionally share data
Browse files Browse the repository at this point in the history
Components should have access to globals, also allowed to explicitly share data with them, which
inturns creates a new copy of data object

Closes #2
  • Loading branch information
thetutlage committed Oct 16, 2017
1 parent dfa3632 commit 5bc9889
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,13 @@ class Template {
* Convert props array to data object
*/
let presenter = null
let shareData = false

const data = _.transform(props, (result, prop) => {
if (prop.presenter) {
presenter = prop.presenter
} else if (prop.shareData) {
shareData = true
} else {
_.merge(result, prop)
}
Expand All @@ -423,7 +427,13 @@ class Template {

const template = new Template(this._tags, this._options, this._globals, this._loader)
template.presenter(presenter)
template._makeContext(data)

if (shareData) {
debug('sharing parent template data with component')
template._makeContext(_.assign({}, this.context.$presenter.$data, data))
} else {
template._makeContext(data)
}
return template
}

Expand Down
1 change: 1 addition & 0 deletions test-helpers/views/components/access-globals.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h2> {{ size(users) }} </h2>
39 changes: 39 additions & 0 deletions test/unit/tags/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,43 @@ test.group('Tags | Component ', (group) => {
const $ = cheerio.load(output)
assert.equal($('h2').text().trim(), 'Hello VIRK')
})

test('should have access to globals', (assert) => {
const template = new Template(this.tags, {}, {
size: function (dict) { return dict.length }
}, loader)
const statement = dedent`
@!component('components.access-globals', users = ['virk'])
`
this.tags.each.run(Context)
const output = template.renderString(statement)
const $ = cheerio.load(output)
assert.equal($('h2').text().trim(), '1')
})

test('share data from the parent view', (assert) => {
const template = new Template(this.tags, {}, {
size: function (dict) { return dict.length }
}, loader)
const statement = dedent`
@!component('components.access-globals', shareData = true)
`
this.tags.each.run(Context)
const output = template.renderString(statement, { users: ['virk', 'nikk'] })
const $ = cheerio.load(output)
assert.equal($('h2').text().trim(), '2')
})

test('should override the parent data view', (assert) => {
const template = new Template(this.tags, {}, {
size: function (dict) { return dict.length }
}, loader)
const statement = dedent`
@!component('components.access-globals', shareData = true, users = ['virk'])
`
this.tags.each.run(Context)
const output = template.renderString(statement, { users: ['virk', 'nikk'] })
const $ = cheerio.load(output)
assert.equal($('h2').text().trim(), '1')
})
})

0 comments on commit 5bc9889

Please sign in to comment.