moving elements and handling events

83 views
Skip to first unread message

Michael "Kato" Wulf

unread,
Sep 19, 2012, 5:03:51 PM9/19/12
to knock...@googlegroups.com
Hello gents,

I'm looking to control some sorted lists using an event-based model (pre-existing). Adding and deleting elements works wonderfully.

However, when an element is moved in the array, this (naturally) triggers a delete followed by an add event. My best guess is to use setTimeout (ick) and simply wait around some small length (20 ms) to see if an add event immediately follows.

This seems a bit sloppy and unreliable (what if there is a lot going on and the timeout is exceeded? what if a large number of moves take place, each delayed by x ms?)

Is there a simple way I could suppress the delete/add notifications and create my own from within knockout? Maybe using an extender?

I'm not looking for anyone to write the code--I'll do my homework and ask questions if I have issues.

I simply want to see if any features or patterns spring to mind that I should investigate or if this is a previously invented wheel.

Cheers,

-- 
Michael Wulf


Michael Best

unread,
Sep 19, 2012, 6:32:27 PM9/19/12
to knock...@googlegroups.com
The next version of Knockout, 2.2, will include beforeMove and afterMove events for moved items. This is already included in the code available in GitHub (https://github.com/SteveSanderson/knockout), which you can use to build the latest Knockout. In your case, if you want to respond to only a single event, you could just pick either one. One caveat, though, is that deleting or adding an item will cause other items to move, triggering move events for them.

-- Michael

Michael "Kato" Wulf

unread,
Sep 19, 2012, 6:40:08 PM9/19/12
to knock...@googlegroups.com
Is there also a move method then? Or does 2.2 utilize a similar pattern to what I was looking for, reading a delete/add of the same record as a move? (right now I'm using splice() to move the records--I assume ko is taking a similar approach)

Michael Best

unread,
Sep 19, 2012, 9:42:31 PM9/19/12
to knock...@googlegroups.com
There is not a separate move method for observableArray.

In 2.2, the foreach binding will treat an item that has a different index in the array (from the last time the binding was updated) as a moved item.

-- Michael
Reply all
Reply to author
Forward
Message has been deleted
0 new messages