Skip to content Skip to sidebar Skip to footer

Knockout Draggable Sortable Mapping Clone Alteration

I am trying to use knockout-sortable, with the mapping plugin, to drag a list of products to another list. Here is a very stripped-back fiddle to try and show what I'm getting at.

Solution 1:

You would want your mapping options to look something like:

var options = {
    productArray: {
        create: function(mappingData) {
            var result = ko.mapping.fromJS(mappingData.data);

            result.clone = function() {
                return { productID: result.productID() };
            };

            return result;
        }
    }
};

Alternatively, you could create a constructor for your product with a clone function on it and just return that like return new Product(mappingData.data);

I had the clone above just return the productID as a non-observable, which should be fine unless you need to react to that value changing.

The first line of your calculatedName function would then need to be something like:

var x = ko.unwrap(arg.productID);

This will retrieve the value whether it is observable or not, so the function can work against the productArray or the listArray.

http://jsfiddle.net/rniemeyer/hLqctg4L/


Post a Comment for "Knockout Draggable Sortable Mapping Clone Alteration"