Opened 6 years ago

Closed 6 years ago

#16632 closed defect (wontfix)

StatefulArray has no notification for items removed by splice

Reported by: ben hockey Owned by: Ed Chatelain
Priority: undecided Milestone: tbd
Component: DojoX MVC Version: 1.8.3
Keywords: Cc:
Blocked By: Blocking:

Description

elements added by splice cause notifications for the added elements but when elements are removed by splice, there are no notifications about the change in values at specific indices in the array (just a notification about the change in length). for example (http://jsfiddle.net/neonstalwart/2rEfq/):

var arr = new StatefulArray(['alice', 'bob']);
        
arr.watch(function (prop, old, value) {
    console.log('%s changed', prop, old, value);
});
        
// add charles
arr.splice(arr.length, 1, 'charles'); // notification received about the addition
        
// remove bob.
arr.splice(1, 1); // notification missing about value change 'bob' -> 'charles'
        
console.log(arr);

based on the API docs i was expecting notifications for the removed elements in the same way as the added elements.

splice() - Stateful update is done for the removed/added elements, as well as the length.

Change History (4)

comment:1 Changed 6 years ago by Ed Chatelain

Does using watchElements instead of watch on the StatefulArray? give you what you want? like this: arr.watchElements(function(idx, removals, adds){

console.log('%s changed', idx, removals, adds);

}

comment:2 Changed 6 years ago by ben hockey

using watchElements sort of does what i want but regardless, notifications for value changes at specific element indexes from splice are generally unreliable right now. i've added another example to http://jsfiddle.net/neonstalwart/2rEfq/1/

var arr = new StatefulArray(['alice', 'bob']);

// <snip>

// replace bob with bill
arr.splice(1, 1, 'bill'); // notification received that index 1 changed undefined -> 'bill' ???

this splice generates a notification that index 1 changed from undefined to 'bill'. this brings to light 2 issues:

  • the notification should be that it changed from 'bob' to 'bill'.
  • additions trigger notifications for changes to values at specific indexes but removals don't cause a similar notification

do you think you could figure out something consistent for the notifications?

my use case is that i have a list of data displayed with an input for each element in the data. each input is watching a specific index in the StatefulArray?. this is achieved with something like sync(values, index, widget, 'value'). visually it's represented like this

  [ input bound to index 0, value = 'alice'   ]
  [ input bound to index 1, value = 'bob'     ]
  [ input bound to index 2, value = 'charles' ]
  [ an empty input for adding to the list     ]

it's possible that someone will remove an element in the middle of the list and so i was planning to use watchElements to figure out the net change in the number of elements and then add/remove widgets.

based on my visual representation above, if someone removed 'bob', i have code that does the equivalent of values.splice(values.indexOf('bob'), 1). in my handler for watchElements i figure out that the net change is one element was removed so i destroy the widget bound to index 2. at that point i was expecting that the element bound to index 1 would get a notification that the new value at index 1 is 'charles'. the result should be

  [ input bound to index 0, value = 'alice'   ]
  [ input bound to index 1, value = 'charles' ]
  [ an empty input for adding to the list     ]

currently i have code in watchElements that updates all the widgets that should have been notified but i'd rather not repeat that everywhere i need splice notifications.

comment:3 Changed 6 years ago by Ed Chatelain

In [30655]:

fixes #16742 Updates for StatefulArray? to fix a memory leak, and to improve the in-line doc. refs#16632 Thanks Akira Sudoh (IBM, CCLA). Also fixed a couple of the WidgetList? tests. !strict

comment:4 Changed 6 years ago by Ed Chatelain

Resolution: wontfix
Status: newclosed

The doc has been improved, closing this as wontfix due to the possibility of hundreds of notifications when an item at the beginning is added or removed.

Note: See TracTickets for help on using tickets.