Skip to content

Commit

Permalink
feat(component): add support for define presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 22, 2017
1 parent 6b6d1d2 commit 7a90885
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Lexer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ class Lexer {
return 'this.runTimeRender'
}

/**
* The method to be used for rendering
* the template with pre-existing
* context.
*
* @attribute renderWithContextFn
*
* @return {String}
*/
get renderWithContextFn () {
return 'this.renderWithContext'
}

/**
* Validates the current type against the allowed types.
* It calls the callback instead of throwing exception
Expand Down
2 changes: 1 addition & 1 deletion src/Tags/ComponentTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ComponentTag extends BaseTag {
* Render the component in runtime, since component name can
* be dynamic aswell.
*/
buffer.writeToOutput(`$\{this.renderWithContext(${name})}`, false)
buffer.writeToOutput(`$\{${lexer.renderWithContextFn}(${name})}`, false)

/**
* End the isolation
Expand Down
8 changes: 7 additions & 1 deletion src/Template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,18 @@ class Template {
/**
* Convert props array to data object
*/
let presenter = null
const data = _.transform(props, (result, prop) => {
_.merge(result, prop)
if (prop.presenter) {
presenter = prop.presenter
} else {
_.merge(result, prop)
}
return result
}, {})

const template = new Template(this._tags, this._globals, this._loader)
template.presenter(presenter)
template._makeContext(data)
return template
}
Expand Down
15 changes: 14 additions & 1 deletion test/unit/tags/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const Context = require('../../../src/Context')
const Template = require('../../../src/Template')
const Loader = require('../../../src/Loader')
const dedent = require('dedent-js')
const loader = new Loader(require('path').join(__dirname, '../../../test-helpers/views'))
const path = require('path')
const loader = new Loader(path.join(__dirname, '../../../test-helpers/views'))

test.group('Tags | Component ', (group) => {
group.beforeEach(() => {
Expand Down Expand Up @@ -322,4 +323,16 @@ test.group('Tags | Component ', (group) => {
assert.equal($('.col-lg-6:first-child h4').text().trim(), 'foo')
assert.equal($('.col-lg-6:last-child h4').text().trim(), 'bar')
})

test('pass presenter to component', (assert) => {
loader.presentersPath = path.join(__dirname, '../../../test-helpers/presenters')
const template = new Template(this.tags, {}, loader)
const statement = dedent`
@!component('components.user', presenter = 'User', username = 'virk')
`
this.tags.each.run(Context)
const output = template.renderString(statement)
const $ = cheerio.load(output)
assert.equal($('h2').text().trim(), 'Hello VIRK')
})
})

0 comments on commit 7a90885

Please sign in to comment.