Knockoutjs foreach custom bindinghandler with afterAdd

前端 未结 2 1400
遇见更好的自我
遇见更好的自我 2021-02-10 21:22

I would like to build a custom bindingHandler

ko.bindingHandlers.foreachWithHighlight that has an highlight effect when afterAdd.

From documentation

<         


        
2条回答
  •  甜味超标
    2021-02-10 21:47

    I've modified one of the knockout tutorials, it doesn't use a custom binding though but still drives the behaviour via beforeRemove and afterAdd which hook into the observableArray.

    data-bind='template: { 
        foreach: planetsToShow,
        beforeRemove: hidePlanetElement,
        afterAdd: showPlanetElement 
    }'
    

    Depending on compatibility you could use css3 transitions as I've done here to give the newly added element a yellow colour that transitions to the normal one after a given timing. (Although the timings and colourings I used need work ;))

    In this fiddle it seems to be working by not applying to the original list, but I guess this might be down to at what stage the observableArray gets modified?

    http://jsfiddle.net/8k8V5/1529/

提交回复
热议问题