Skip to content

Commit

Permalink
Let component viewmodels be simpler by not having to check whether 'p…
Browse files Browse the repository at this point in the history
…arams' is null
  • Loading branch information
SteveSanderson committed Jun 11, 2014
1 parent 9143516 commit b7fe352
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
26 changes: 26 additions & 0 deletions spec/components/customElementBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,32 @@ describe('Components: Custom elements', function() {
expect(suppliedParams).toEqual([{ nothing: null, num: 123, bool: true, obj: { abc: 123 }, str: 'mystr' }]);
});

it('Supplies an empty params object (with empty $raw) if a custom element has no params attribute', function() {
var suppliedParams = [];
ko.components.register('test-component', {
template: 'Ignored',
viewModel: function(params) { suppliedParams.push(params); }
});

testNode.innerHTML = '<test-component></test-component>';
ko.applyBindings(null, testNode);
jasmine.Clock.tick(1);
expect(suppliedParams).toEqual([{ $raw: {} }]);
});

it('Supplies an empty params object (with empty $raw) if a custom element has an empty whitespace params attribute', function() {
var suppliedParams = [];
ko.components.register('test-component', {
template: 'Ignored',
viewModel: function(params) { suppliedParams.push(params); }
});

testNode.innerHTML = '<test-component params=" "></test-component>';
ko.applyBindings(null, testNode);
jasmine.Clock.tick(1);
expect(suppliedParams).toEqual([{ $raw: {} }]);
});

it('Should not confuse parameters with bindings', function() {
this.restoreAfter(ko, 'getBindingHandler');
var bindings = [];
Expand Down
5 changes: 4 additions & 1 deletion src/components/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@

return result;
} else {
return null;
// For consistency, absence of a "params" attribute is treated the same as the presence of
// any empty one. Otherwise component viewmodels need special code to check whether or not
// 'params' or 'params.$raw' is null/undefined before reading subproperties, which is annoying.
return { '$raw': {} };
}
}

Expand Down

0 comments on commit b7fe352

Please sign in to comment.