locked
use List.setAt without recreating entire complex object

    Question

  • Hi there

    so I know that if I want to use setAt I can do something like this:

    var item = list.getAt(eventObject.detail.itemIndex);
                var count = item.countnum + 1;
                var updatedItem = {
                    group: item.group,
                    title: item.title,
                    countnum: count,
                    backgroundColor: "rgba(190,180,220,0.6)"
                };


    Is there any way to use List.setAt without having to recreate the entire object? The reason I'm trying to avoid having to recreate the object in code is that the object is fairly complex and updates quite often for our service so I'd like to avoid having a bunch of static code that recreate an object that can have properties added to it on a regular basis.

    Tuesday, December 11, 2012 3:29 PM

Answers

  • Hi,

    Please refer to the code as follow:

    itemInvoked: function (eventObject) {
    
                msSetImmediate(function (arg) {
                    var item = data.items.getAt(arg.detail.itemIndex);
                    var count = item.count + 1;
                    var updatedItem = {
                        group: item.group,
                        title: item.title,
                        count: count,
                        description: item.description,
                        content: item.content,
                        backgroundImage: item.backgroundImage
                    };
    
                    data.items.setAt(arg.detail.itemIndex, updatedItem);
            }, eventObject);
           
    
            }


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Thursday, December 13, 2012 2:03 AM
    Wednesday, December 12, 2012 2:21 AM
  • yeah so that's not really a great solution since I have to manually recreate the object which would lead to a lot of code maintenance if I'm working with complex objects that get updated often and wouldn't work at all with dynamic objects.

    Fortunately I've found a better solution, which is to use underscore.extend

    var amountChanged = function (newAmount, indexNumber) {
    
          var updatedItem = _({}).extend(item);
          updatedItem.root.amount.amount = newAmount;
    
          currentTrxn.setAt(indexNumber, updatedItem);
          console.log(currentTrxn);
        }


    • Marked as answer by Syllogism Wednesday, December 12, 2012 7:32 AM
    • Edited by Syllogism Wednesday, December 12, 2012 7:33 AM
    Wednesday, December 12, 2012 7:32 AM

All replies

  • Hi,

    Please refer to the code as follow:

    itemInvoked: function (eventObject) {
    
                msSetImmediate(function (arg) {
                    var item = data.items.getAt(arg.detail.itemIndex);
                    var count = item.count + 1;
                    var updatedItem = {
                        group: item.group,
                        title: item.title,
                        count: count,
                        description: item.description,
                        content: item.content,
                        backgroundImage: item.backgroundImage
                    };
    
                    data.items.setAt(arg.detail.itemIndex, updatedItem);
            }, eventObject);
           
    
            }


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Song Tian Thursday, December 13, 2012 2:03 AM
    Wednesday, December 12, 2012 2:21 AM
  • yeah so that's not really a great solution since I have to manually recreate the object which would lead to a lot of code maintenance if I'm working with complex objects that get updated often and wouldn't work at all with dynamic objects.

    Fortunately I've found a better solution, which is to use underscore.extend

    var amountChanged = function (newAmount, indexNumber) {
    
          var updatedItem = _({}).extend(item);
          updatedItem.root.amount.amount = newAmount;
    
          currentTrxn.setAt(indexNumber, updatedItem);
          console.log(currentTrxn);
        }


    • Marked as answer by Syllogism Wednesday, December 12, 2012 7:32 AM
    • Edited by Syllogism Wednesday, December 12, 2012 7:33 AM
    Wednesday, December 12, 2012 7:32 AM