stuyam / pressure

:point_down::boom: JavaScript library for handling Force Touch, 3D Touch, and Pointer Pressure.
https://pressurejs.com
MIT License
2.91k stars 98 forks source link

Integration with KnockoutJS? #106

Closed PhonicUK closed 1 year ago

PhonicUK commented 1 year ago

How can I integrate this with KnockoutJS so that newly created elements via a template get registered and so I can use the force-push event in Knockout binding?

PhonicUK commented 1 year ago

I only implemented deepStartPress but here is a nice way to use it with KO bindings:

ko.bindingHandlers.forcePush = {
    init: function (element, valueAccessor, _allBindings, viewModel) {
        var callback = valueAccessor();
        Pressure.set(element, {
            startDeepPress: function (event) {
                callback.call(viewModel, element, event);
            },
        });
    },
};

Then you can do

<div data-bind="forcePush: menu">