Skip to content

Commit

Permalink
Merge pull request #28 from joonas-lahtinen/master
Browse files Browse the repository at this point in the history
Update dist & support objects which do not have Object as prototype
  • Loading branch information
shannonmoeller committed Nov 14, 2015
2 parents bfff859 + 53462ef commit b9e6675
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
29 changes: 18 additions & 11 deletions dist/handlebars-layouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,21 @@ function getActionsByName(context, name) {
}

function applyAction(val, action) {
var context = this;

function fn() {
return action.fn(context, action.options);
}

switch (action.mode) {
case 'append': {
return val + action.fn(this);
return val + fn();
}
case 'prepend': {
return action.fn(this) + val;
return fn() + val;
}
case 'replace': {
return action.fn(this);
return fn();
}
default: {
return val;
Expand All @@ -64,7 +70,7 @@ function mixin(target) {

for (key in arg) {
// istanbul ignore else
if (arg.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(arg, key)) {
target[key] = arg[key];
}
}
Expand Down Expand Up @@ -101,12 +107,10 @@ function layouts(handlebars) {
options = options || {};

var fn = options.fn || noop,
context = handlebars.createFrame(this || {}),
context = mixin({}, this, customContext, options.hash),
data = handlebars.createFrame(options.data),
template = handlebars.partials[name];

// Mix custom context and hash into context
mixin(context, customContext, options.hash);

// Partial template required
if (template == null) {
throw new Error('Missing partial: \'' + name + '\'');
Expand All @@ -121,7 +125,7 @@ function layouts(handlebars) {
getStack(context).push(fn);

// Render partial
return template(context);
return template(context, { data: data });
},

/**
Expand All @@ -134,7 +138,7 @@ function layouts(handlebars) {
* @return {String} Rendered partial.
*/
embed: function () {
var context = handlebars.createFrame(this || {});
var context = mixin({}, this || {});

// Reset context
context.$$layoutStack = null;
Expand All @@ -155,13 +159,14 @@ function layouts(handlebars) {
options = options || {};

var fn = options.fn || noop,
data = handlebars.createFrame(options.data),
context = this || {};

applyStack(context);

return getActionsByName(context, name).reduce(
applyAction.bind(context),
fn(context)
fn(context, { data: data })
);
},

Expand All @@ -178,6 +183,7 @@ function layouts(handlebars) {
options = options || {};

var fn = options.fn,
data = handlebars.createFrame(options.data),
hash = options.hash || {},
mode = hash.mode || 'replace',
context = this || {};
Expand All @@ -191,6 +197,7 @@ function layouts(handlebars) {

// Setter
getActionsByName(context, name).push({
options: { data: data },
mode: mode.toLowerCase(),
fn: fn
});
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function mixin(target) {

for (key in arg) {
// istanbul ignore else
if (arg.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(arg, key)) {
target[key] = arg[key];
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/expected/templates/non-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctype html>
<html lang="en-us">
<head>
<title></title>

<link rel="stylesheet" href="assets/css/screen.css" />
</head>
<body>
<div class="site">

<div class="site-bd" role="main">
<p>value</p>
</div>

</div>

<script src="assets/js/controllers/home.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/templates/non-object.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{#extend "layout"}}
{{#content "body"}}
<p>{{ key }}</p>
{{/content}}
{{/extend}}
7 changes: 7 additions & 0 deletions test/handlebars-layouts.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ describe('handlebars-layouts e2e', function () {
testWithFile('hash.html', data, done);
});

it('should pass through non-object values', function (done) {
var data = Object.create(null);

data.key = 'value';
testWithFile('non-object.html', data, done);
});

it('should throw an error if partial is not registered', function (done) {
testWithFile('error.html', {}, done);
});
Expand Down

0 comments on commit b9e6675

Please sign in to comment.