locked
update WinJS.Binding.List on Splice

    Question

  • Hello,

    I am building an App similar to the Grid App. For one of my pages lots of my items have the same value and I have returned just 1 item for each of these different values using the .createFiltered method. This is working great for adding new items because the .createFiltered method updates and checks to see if my new item has a new value.

    The issue I am having is with removing items. When I splice the WinJS list there is a chance it is the removing the item which which the .createFiltered method has returned for the repeated value, but there are still other items about with that value. So i need the .createFiltered to run again and then pick a different item.

    I will give you an example.

    I have 5 items with 2 types's between them "Apple" and "banana".

    On my home page I have sorted the items into Fruit / Vegetables / Sweets. These 5 items all come under fruit but instead of giving the different brands I just want it to say "Apple" and "Banana" so I have a function that will get one item for each type and bind it to a WinJS.Binding.Template and then use "textContent: Type" to display Apple / Banana.

    Say I have 2 apples "Gala" and "Granny Smith" and I have bound the data of "Gala" to the home page and I then run out of "Gala" Apples (deleting it). I still have Apples in stock (Granny Smith) so I need to .createFiltered function to run again as the Home Page no longer displays "Apples" under Fruit.

    Thank you


    • Edited by r.c.byrne Wednesday, January 01, 2014 10:07 AM
    Wednesday, January 01, 2014 9:28 AM

Answers

  • Since writing this I have managed to find a way to get it to work. What I have done is found the value of the item I am deleting and then gone through the array to see if I can find another item with that value. I have then removed that from the list and then re-added it causing .createFiltered to process the item. 

    If anyone has a simpler solution I would love to hear it :)

    This is the code I used in the function:

        function dashRefresh(toDelete) {
            //Project Key of Deleted Item
            var deletedProjectKey = toDelete.data.project.key;
            //Use new item to bind project data
            var newProjectBinder = 0;
            var newProjectBinderIndex;
    
            for (var i = 0; i < list.length; i++) {
                var listItem = list.getAt(i);
                if (listItem.project.key == deletedProjectKey) {
                    newProjectBinder = listItem;
                    newProjectBinderIndex = i;
                    i = list.length;
                }
            }
    
            if (newProjectBinder != 0) {
                list.splice(newProjectBinderIndex, 1);
                list.push(newProjectBinder);
            }
        }

    • Edited by r.c.byrne Wednesday, January 01, 2014 12:18 PM
    • Marked as answer by r.c.byrne Thursday, January 02, 2014 3:22 PM
    Wednesday, January 01, 2014 12:14 PM

All replies

  • Since writing this I have managed to find a way to get it to work. What I have done is found the value of the item I am deleting and then gone through the array to see if I can find another item with that value. I have then removed that from the list and then re-added it causing .createFiltered to process the item. 

    If anyone has a simpler solution I would love to hear it :)

    This is the code I used in the function:

        function dashRefresh(toDelete) {
            //Project Key of Deleted Item
            var deletedProjectKey = toDelete.data.project.key;
            //Use new item to bind project data
            var newProjectBinder = 0;
            var newProjectBinderIndex;
    
            for (var i = 0; i < list.length; i++) {
                var listItem = list.getAt(i);
                if (listItem.project.key == deletedProjectKey) {
                    newProjectBinder = listItem;
                    newProjectBinderIndex = i;
                    i = list.length;
                }
            }
    
            if (newProjectBinder != 0) {
                list.splice(newProjectBinderIndex, 1);
                list.push(newProjectBinder);
            }
        }

    • Edited by r.c.byrne Wednesday, January 01, 2014 12:18 PM
    • Marked as answer by r.c.byrne Thursday, January 02, 2014 3:22 PM
    Wednesday, January 01, 2014 12:14 PM
  • Thank you for sharing the solution with us. Your solution must help someone also seeking for help on this issue.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, January 03, 2014 2:36 AM
    Moderator