Skip to content

Commit

Permalink
Fix cannot override jade.locals
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisyip committed Nov 28, 2015
1 parent 04aefc9 commit d8a7d4a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Expand Up @@ -17,7 +17,7 @@
"no-nested-ternary": 1,
"curly": 2,
"camelcase": 0,
"no-undefined": 0,
"no-undef": 2,
"no-multi-spaces": 2,
"wrap-iife": [2, "inside"],
"generator-star-spacing": [
Expand Down Expand Up @@ -49,7 +49,7 @@
"no-catch-shadow": 2,
"handle-callback-err": 0,
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
"eqeqeq": 2,
"eqeqeq": [2, "smart"],
"consistent-return": 0,
"no-underscore-dangle": 0,
"no-shadow": 0,
Expand Down
6 changes: 0 additions & 6 deletions example/app.js
Expand Up @@ -19,12 +19,6 @@ var jade = new Jade({

jade.locals.github = '//github.com/chrisyip'

app.use(function* (next) {
try {
yield next
} catch (e) {}
})

app.use(jade.middleware)

app.use(function* (next) {
Expand Down
28 changes: 13 additions & 15 deletions index.js
Expand Up @@ -37,14 +37,14 @@ function loadHelpers (dirs) {
load(dir + '/' + file)
})
} else if (stat.isFile()) {
module = require(fullPath)
var mod = require(fullPath)

if (_.isString(moduleName)) {
helpers[moduleName] = module
} else if (_.isString(module.moduleName)) {
helpers[module.moduleName] = module.moduleBody
helpers[moduleName] = mod
} else if (_.isString(mod.moduleName)) {
helpers[mod.moduleName] = mod.moduleBody
} else {
helpers[_.camelCase(path.basename(fullPath, path.extname(fullPath)))] = module
helpers[_.camelCase(path.basename(fullPath, path.extname(fullPath)))] = mod
}
}
}
Expand All @@ -61,6 +61,7 @@ function Jade (options) {
var compilers = new Map()
var defaultLocals = {}
var viewPath
var helpers = {}

/**
* @param {String} tpl the template path, search start from viewPath
Expand Down Expand Up @@ -114,7 +115,7 @@ function Jade (options) {
}
}

this.body = compiler(_.merge({}, defaultLocals, this.state, locals))
this.body = compiler(_.merge({}, helpers, defaultLocals, this.state, locals))
this.type = 'text/html'
return this
}
Expand All @@ -140,10 +141,7 @@ function Jade (options) {
}

if (_.isEmpty(options)) {
defaultOptions = {
compileDebug: false,
pretty: false
}
defaultOptions = {}
return
}

Expand All @@ -158,7 +156,7 @@ function Jade (options) {
}

if (_.isString(options.helperPath) || _.isArray(options.helperPath)) {
_.merge(defaultLocals, loadHelpers(options.helperPath))
_.merge(helpers, loadHelpers(options.helperPath))
}

if (_.isBoolean(options.debug)) {
Expand All @@ -185,11 +183,11 @@ function Jade (options) {
},

set: function (val) {
if (!_.isPlainObject(val)) {
return
if (val == null) {
defaultLocals = {}
} else if (_.isPlainObject(val)) {
defaultLocals = val
}

defaultLocals = {}
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -35,7 +35,7 @@
"bluebird": "^2.9.34",
"chai": "^3.2.0",
"cheerio": "^0.19.0",
"eslint": "^1.3.1",
"eslint": "^1.10.2",
"istanbul": "^0.3.19",
"jstransformer-markdown-it": "^0.1.0",
"koa": "^1.0.0",
Expand Down
15 changes: 15 additions & 0 deletions test/index.js
@@ -1,9 +1,12 @@
/* eslint-env mocha */

var app = require('../example/app')
var request = require('supertest-koa-agent')
var $ = require('cheerio')
var Promise = require('bluebird')
var Jade = require('..')
require('chai').Should()
var expect = require('chai').expect

describe('koa-jade', function () {
it('should render Jade file', function (done) {
Expand Down Expand Up @@ -99,6 +102,18 @@ describe('koa-jade', function () {
jade.locals.should.be.an.Object
})

it('should override original value', function () {
var jade = new Jade({ locals: { foo: 'bar' } })
jade.locals = { baz: 'baz' }

expect(jade.locals.foo).to.not.exist
expect(jade.locals.baz).to.eql('baz')

jade.locals = null
expect(Object.keys(jade.locals).length).to.eql(0)
expect(jade.locals.baz).to.not.exist
})

it('should be manipulatable', function (done) {
request(app).get('/')
.expect(function (res) {
Expand Down

0 comments on commit d8a7d4a

Please sign in to comment.