Excluding properties in toJSON/toJS?

542 views
Skip to first unread message

Johan Nordberg

unread,
Apr 6, 2011, 3:57:49 PM4/6/11
to knock...@googlegroups.com
Is there a way to exclude properties from being serialized with toJSON? I think I've read somethere that the mapping plugin might handle this?

The problem I have is that a model with children[] / parent properties have circular references when serializing. I'd like to exclude the parent from being serialized.

// Johan

nevf

unread,
Apr 6, 2011, 6:47:46 PM4/6/11
to KnockoutJS
The way I've handled this is using ko.toJS() and deleting the
properties I don't want included.

var tvModel = ko.toJS( viewModel );
delete tvModel.app_name_values;
delete tvModel.common_items.background_image_values;
// ...
var json = JSON.stringify( tvModel, null, 4 ));

- Neville http://www.surfulater.com

abp

unread,
Apr 7, 2011, 6:07:18 AM4/7/11
to KnockoutJS
I filter my objects with a helper function and an empty reference
object, initially rendered by the server:
function filterObj(obj, filter)
{
var result;
if(typeof obj === "object")
{
result = {};
for(key in obj)
{
if(filter[key] !== undefined)
{
result[key] = filterObj(obj[key], filter[key]);
}
}
}
else
{
result = obj;
}
return result;
}

var filtered = filterObj(data, emptyDataObject);

// convert and send filtered data object(s)

On 7 Apr., 00:47, nevf <s...@surfulater.com> wrote:
> The way I've handled this is using ko.toJS() and deleting the
> properties I don't want included.
>
>                 var tvModel = ko.toJS( viewModel );
>                 delete tvModel.app_name_values;
>                 delete tvModel.common_items.background_image_values;
>                 // ...
>                 var json = JSON.stringify( tvModel, null, 4 ));
>
> - Nevillehttp://www.surfulater.com
Reply all
Reply to author
Forward
0 new messages