Knockout mapping and parent options

626 views
Skip to first unread message

Filipe Pinheiro

unread,
Aug 6, 2012, 7:52:04 AM8/6/12
to knock...@googlegroups.com
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

Roy Jacobs

unread,
Aug 6, 2012, 8:02:36 AM8/6/12
to knock...@googlegroups.com
Hi Filipe,

It has been an outstanding issue for a while that the 'options.parent' property is not always consistent. In your fiddle it can be solved by calling "vm.items.mappedRemove" instead, but I realize that may not be possible in your actual app.

I will take a look at fixing this, though.

Roy

Filipe Pinheiro

unread,
Aug 6, 2012, 8:11:34 AM8/6/12
to knock...@googlegroups.com
I will try a workaround and follow close the developments.

Thank you.

Roy Jacobs

unread,
Aug 6, 2012, 8:26:55 AM8/6/12
to knock...@googlegroups.com
Hi Filipe,

I have just committed a fix to GitHub. It makes your jsfiddle example work without any changes. Let me know if you run into additional issues by adding an issue on GitHub.

With regards,
Roy
Reply all
Reply to author
Forward
0 new messages