Cloning an object with Knockout that has an observable array

Alex

I have a view model called ProductsViewModel This contains an observableArray of ProductViewModel

A ProductViewModel also contains an observableArray - of ProductPriceViewModel

One feature I have is that I can duplicate a ProductViewModel and insert it into the ProductsViewModel array.

When I clone using:

ko.mapping.fromJS(ko.toJS(itemToCopy));

It doesn't appear to copy correctly - the prices observable array, isn't populated with ProductPriceViewModels - just Object

Here's the view models

var ProductsViewModel = function() {
    var self = this;

    self.products = ko.observableArray([new ProductViewModel()]);

    self.addNewProduct = function() {
        self.products.push(new ProductViewModel());
    };

    self.duplicateProduct = function() {
        var itemToCopy = ko.utils.arrayFirst(self.products(), function(item) {
            return item.visible();
        });

        //if i look at itemToCopy.prices() it is an array of ProductViewModel

        var newItem = ko.mapping.fromJS(ko.toJS(itemToCopy));
        //if i look at newItem.prices() it is an array of Object

        self.products.push(newItem);
    };
};

var ProductViewModel = function() {
    var self = this;

    self.name = ko.observable();
    self.visible = ko.observable(true);

    self.prices = ko.observableArray([new ProductPriceViewModel()]);

    self.addPrice = function() {
        self.prices.push(new ProductPriceViewModel());
    };
};

var ProductPriceViewModel = function() {
    var self = this;

    self.name = ko.observable();
    self.price = ko.observable();
};
Alex

I solved this by passing in a mapping configuration like this:

var mapping = {
    'prices': {
        create: function (options) {
            return new ServicePriceViewModel(options.data);
        }
    }
};

on

var newItem = ko.mapping.fromJS(ko.toJS(productToCopy), mapping);

and changing my ProductPriceViewModel to accept data as a parameter:

var ProductPriceViewModel = function (data) {
    var self = this;

    self.name = ko.observable();
    self.description = ko.observable();
    self.price = ko.observable();
    self.priceIsFrom = ko.observable();

    if (data)
        ko.mapping.fromJS(data, {}, this);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

knockout add javascript object to an observable array

From Dev

knockout dynamically replace a object in an observable array

From Dev

Knockout access object values from observable array

From Dev

Knockout Observable Array of Observable Array

From Dev

Knockout and observable array mismatch

From Dev

Knockout: Link observable array

From Dev

Knockout Filtering on Observable Array

From Dev

Knockout search in observable array

From Dev

knockout observable array computed

From Dev

Knockout and observable array mismatch

From Dev

'QuerySet' object has no attribute '_cloning'

From Dev

'QuerySet' object has no attribute '_cloning'

From Dev

Skipping an $index() in a Knockout Observable Array

From Dev

Add item to Observable Array Knockout

From Dev

unable to bind observable array - knockout

From Dev

Mapping JSON into Knockout Observable Array

From Dev

Displaying contents of a knockout observable array

From Dev

push json to knockout observable array

From Dev

Observable Array Sort Issue with Knockout

From Dev

Knockout pushing observable and computed data to an observable array

From Dev

Trouble deep cloning an array object

From Dev

Knockout JS Setting Initial value for dropdown from observable array object property value

From Dev

Knockout Subscribe to any change in observable complex object

From Dev

Is there any way to get the name of an Knockout observable object?

From Dev

Array of JSON Objects to Knockout Observable Array With Observable Properties

From Dev

Knockout computed not updating using observable array

From Dev

knockout js observable array removing all items

From Dev

Knockout JS: Passing observable array as argument

From Dev

Creating an observable knockout array from a JSON response

Related Related

  1. 1

    knockout add javascript object to an observable array

  2. 2

    knockout dynamically replace a object in an observable array

  3. 3

    Knockout access object values from observable array

  4. 4

    Knockout Observable Array of Observable Array

  5. 5

    Knockout and observable array mismatch

  6. 6

    Knockout: Link observable array

  7. 7

    Knockout Filtering on Observable Array

  8. 8

    Knockout search in observable array

  9. 9

    knockout observable array computed

  10. 10

    Knockout and observable array mismatch

  11. 11

    'QuerySet' object has no attribute '_cloning'

  12. 12

    'QuerySet' object has no attribute '_cloning'

  13. 13

    Skipping an $index() in a Knockout Observable Array

  14. 14

    Add item to Observable Array Knockout

  15. 15

    unable to bind observable array - knockout

  16. 16

    Mapping JSON into Knockout Observable Array

  17. 17

    Displaying contents of a knockout observable array

  18. 18

    push json to knockout observable array

  19. 19

    Observable Array Sort Issue with Knockout

  20. 20

    Knockout pushing observable and computed data to an observable array

  21. 21

    Trouble deep cloning an array object

  22. 22

    Knockout JS Setting Initial value for dropdown from observable array object property value

  23. 23

    Knockout Subscribe to any change in observable complex object

  24. 24

    Is there any way to get the name of an Knockout observable object?

  25. 25

    Array of JSON Objects to Knockout Observable Array With Observable Properties

  26. 26

    Knockout computed not updating using observable array

  27. 27

    knockout js observable array removing all items

  28. 28

    Knockout JS: Passing observable array as argument

  29. 29

    Creating an observable knockout array from a JSON response

HotTag

Archive