SteveSanderson/knockout — Issue #251: ko.toJS() serialize every property including ordinal functions
Author: eldargab
Created: 2011-12-28 15:20:31+00:00
State: open

Hi. I stumbled across funny case while serializing my model and passing it to jQuery.ajax().
The following code suddenly caused stack overflow error (after clicking "save" button).

```html
Save
```

```javascript
function AdvertModel () {
this.name = ko.observable();
this.description = ko.observable();

var self = this;

this.save = function (data, evt) {
var advert = ko.toJS(self);
$.post('/new-advert', advert, function () {
self.clear();
});
}

this.clear = function () {
this.name(null);
this.description(null);
}
}
```

That's because after serializing AdvertModel it's save method was included into result, at the same time jQuery's serialization method treats all functions as getters and invokes them. As a result jQuery calls model's save() method again and we have recursion.

Personally, I would excluded functions automatically (just without thinking). They have no sens as data and the only reason for ko.toJS is to extract data. But I don't insist - "remove functions immediately!". I just wanted to share this issue.