locked
Coyp WinJs Item

    Question

  • How can I duplicate a WinJS item? I want to add a new item to a list which has a lot of similar properties as an existing item, I tried setting a variable equal to a winjs item and then editing the variable, but found out its editing both items.

    var item = list.getAt(0)

    item.title = "Test";

    list.push(item);



    Saturday, May 03, 2014 2:12 PM

All replies

  • The reason why its still referencing to your old item, is because you used an assignment " var item = list.getAt(0), so when you change the title, you are still referencing the old data. One way get around this is creating a new instance of the object and pushing that information in. Here's a quick example:

    var item = list.getAt(0);
    var itemInstance = function(title,subtitle){
        this.title = title;
        this.subtitle = subtitle;
        //you can add more properties if you like
    }
    var temp = new itemInstance('NewTitle',item.subtitle)
    list.push(temp);

    Hope this helps!

    • Proposed as answer by datQkiD2 Saturday, May 03, 2014 4:19 PM
    • Unproposed as answer by datQkiD2 Sunday, May 04, 2014 1:12 AM
    Saturday, May 03, 2014 4:17 PM