Skip to content

Commit

Permalink
feat(view): add globals for linkTo and linkToAction
Browse files Browse the repository at this point in the history
linkTo will create an anchor tag for a route and linkToAction will be used for controller actions
  • Loading branch information
thetutlage committed Apr 27, 2016
1 parent 8f4c11d commit 3e6530d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/View/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ const Form = require('./Form')

module.exports = function (env, Route) {
env.addGlobal('form', new Form(env, Route))

env.addGlobal('linkTo', function (route, text, options, target) {
const url = env.filters.route(route, options)
target = target ? `target="${target}"` : ''
return env.filters.safe(`<a href="${url}" ${target}> ${text} </a>`)
})

env.addGlobal('linkToAction', function (action, text, options, target) {
const url = env.filters.action(action, options)
target = target ? `target="${target}"` : ''
return env.filters.safe(`<a href="${url}" ${target}> ${text} </a>`)
})
}
24 changes: 24 additions & 0 deletions test/unit/view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ describe('View',function () {
expect(profile.trim()).to.equal('/1')
})

it('should make an anchor link to a given route', function * () {
Route.get('/:id','ProfileController.show').as('profile')
const viewString = this.view.makeString('{{ linkTo("profile", "View Profile", {id: 1}) }}')
expect(viewString.trim()).to.equal('<a href="/1" > View Profile </a>')
})

it('should make an anchor link with defined target to a given route', function * () {
Route.get('/users','ProfileController.show').as('listUsers')
const viewString = this.view.makeString('{{ linkTo("listUsers", "View Profile", {}, "_blank") }}')
expect(viewString.trim()).to.equal('<a href="/users" target="_blank"> View Profile </a>')
})

it('should make an anchor link to a given action', function * () {
Route.get('profile/:id','ProfileController.show')
const viewString = this.view.makeString('{{ linkToAction("ProfileController.show", "View Profile", {id: 1}) }}')
expect(viewString.trim()).to.equal('<a href="/profile/1" > View Profile </a>')
})

it('should make an anchor link with defined target to a given action', function * () {
Route.get('profile/:id','ProfileController.show')
const viewString = this.view.makeString('{{ linkToAction("ProfileController.show", "View Profile", {id: 1}, "_blank") }}')
expect(viewString.trim()).to.equal('<a href="/profile/1" target="_blank"> View Profile </a>')
})

it('should return empty string when unable to find route action', function * () {
const profile = this.view.makeString('{{ "ProfileController.show" | action({id:1}) }}',{id:1})
expect(profile.trim()).to.equal('')
Expand Down

0 comments on commit 3e6530d

Please sign in to comment.