Skip to content

Commit

Permalink
refactor(view): move inbuilt globals to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 27, 2016
1 parent d45bbab commit 8f4c11d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/View/Form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const _ = require('lodash')
*/
class Form {

constructor (View, Route) {
this.env = View.viewsEnv
constructor (viewsEnv, Route) {
this.env = viewsEnv
this.specialKeywords = ['url', 'files', 'method', 'route', 'action', 'params']
this.validFormMethods = ['GET', 'POST']
this.route = Route
Expand Down
16 changes: 16 additions & 0 deletions src/View/globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

/**
* adonis-framework
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const Form = require('./Form')

module.exports = function (env, Route) {
env.addGlobal('form', new Form(env, Route))
}
4 changes: 2 additions & 2 deletions src/View/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

const nunjucks = require('nunjucks')
const ViewLoader = require('./loader')
const Form = require('./Form')
const viewFilters = require('./filters')
const viewExtensions = require('./extensions')
const viewGlobals = require('./globals')

/**
* View class for adonis framework to serve jinja like views
Expand All @@ -26,8 +26,8 @@ class View {
const viewsPath = Helpers.viewsPath()
const viewsCache = Config.get('app.views.cache', true)
this.viewsEnv = new nunjucks.Environment(new ViewLoader(viewsPath, false, !viewsCache))
this.global('form', new Form(this, Route))
viewExtensions(this.viewsEnv)
viewGlobals(this.viewsEnv, Route)
viewFilters(this.viewsEnv, Route)
}

Expand Down
6 changes: 3 additions & 3 deletions test/unit/view.form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Config = {
}
}

const form = new Form(new View(Helpers, Config, Route), Route)
const form = new Form(new View(Helpers, Config, Route).viewsEnv, Route)

describe('Form Helper', function () {
it('should be able to create a form opening tag using open method', function () {
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('Form Helper', function () {
view.global('old', function () {
return 'some value'
})
const formNew = new Form(view, Route)
const formNew = new Form(view.viewsEnv, Route)
const label = formNew.text('username')
expect(label.val).to.equal('<input type="text" name="username" value="some value" id="username" />')
})
Expand All @@ -148,7 +148,7 @@ describe('Form Helper', function () {
view.global('old', function () {
return 'some value'
})
const formNew = new Form(view, Route)
const formNew = new Form(view.viewsEnv, Route)
const input = formNew.text('username', null, {avoidOld: true})
expect(input.val).to.equal('<input type="text" name="username" id="username" />')
})
Expand Down

0 comments on commit 8f4c11d

Please sign in to comment.