Filipe Pinheiro
Filipe Pinheiro

Reputation: 1102

Knockout mapping and parent options

I'm tring to map this

{ items: [
    { id: 1 },
    { id: 2 },
    { id: 3 }
]};

when creating something in the array I add a function to remove the item from the collection.

var mapping = {
    'items': {
        key: function(data) {
            return ko.utils.unwrapObservable(data.id);
        },
        create: function(options) {
            var o = (new(function() {
                this._remove = function() {
                    options.parent.items.mappedRemove(options.data);
                };
                ko.mapping.fromJS(options.data, {}, this);
            })());
            return o;
        }
    }
};

this method works if I am removing an item added using items.mappedCreate but don´t work with the items mapped on ko.mapping.fromJS.

When debugging I noticed that options.parent are not the same in the different situation. Why? Should both methods return as parent the items observableArray?

I have set up a jsfiddle with an example http://jsfiddle.net/fampinheiro/9CcME/.

Thank you

Upvotes: 1

Views: 1110

Answers (1)

Filipe Pinheiro
Filipe Pinheiro

Reputation: 1102

I posted my problem in knockout ggroups

https://groups.google.com/d/topic/knockoutjs/cqBr_CPsfqc/discussion

and Roy Jacobs solved my jsfiddle problem.

Upvotes: 1

Related Questions