Skip to content

Commit

Permalink
ko.toJS should not serialize functions, fixes #251
Browse files Browse the repository at this point in the history
  • Loading branch information
paglias committed Feb 7, 2013
1 parent 2fe1ae1 commit 3cf90eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 13 additions & 1 deletion spec/mappingHelperBehaviors.js
Expand Up @@ -76,7 +76,6 @@ describe('Mapping helpers', function() {
expect(result.due instanceof Date).toEqual(true);
expect(result.due).toEqual(date);

console.log(string instanceof String, result.string instanceof String);
expect(result.string instanceof String).toEqual(true);
expect(result.string).toEqual(string);

Expand All @@ -87,6 +86,19 @@ describe('Mapping helpers', function() {
expect(result.booleanValue).toEqual(booleanValue);
});

it('ko.toJS shouldn\'t serialize functions', function() {
var obj = {
include: ko.observable("I should be serialized"),
exclude: function(){
return "I shouldn't be serialized"
}
};

var result = ko.toJS(obj);
expect(result.include).toEqual("I should be serialized");
expect(result.exclude).toEqual(undefined);
});

it('ko.toJSON should unwrap everything and then stringify', function() {
var data = ko.observableArray(['a', 1, { someProp : ko.observable('Hey') }]);
var result = ko.toJSON(data);
Expand Down
3 changes: 2 additions & 1 deletion src/subscribables/mappingHelpers.js
Expand Up @@ -64,7 +64,8 @@
visitorCallback('toJSON');
} else {
for (var propertyName in rootObject)
visitorCallback(propertyName);
if ( !(typeof rootObject[propertyName] === 'function') || ko.isObservable(rootObject[propertyName]) )
visitorCallback(propertyName);
}
};

Expand Down

0 comments on commit 3cf90eb

Please sign in to comment.