From 3e6530da6832ef9ea3946af61d64e9fd1c0c6775 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Thu, 28 Apr 2016 01:14:30 +0530 Subject: [PATCH] feat(view): add globals for linkTo and linkToAction linkTo will create an anchor tag for a route and linkToAction will be used for controller actions --- src/View/globals.js | 12 ++++++++++++ test/unit/view.spec.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/View/globals.js b/src/View/globals.js index 45d712b2..2dff24d8 100644 --- a/src/View/globals.js +++ b/src/View/globals.js @@ -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(` ${text} `) + }) + + env.addGlobal('linkToAction', function (action, text, options, target) { + const url = env.filters.action(action, options) + target = target ? `target="${target}"` : '' + return env.filters.safe(` ${text} `) + }) } diff --git a/test/unit/view.spec.js b/test/unit/view.spec.js index 12361dda..1382fd88 100644 --- a/test/unit/view.spec.js +++ b/test/unit/view.spec.js @@ -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(' View Profile ') + }) + + 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(' View Profile ') + }) + + 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(' View Profile ') + }) + + 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(' View Profile ') + }) + 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('')