Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply jquery effect to element created by Knockout.js

Hello I have the following code in my view:

        <div data-bind="foreach: Elements">
            <div data-bind="attr:{id: id}">
                <img data-bind="attr:{src: ImageSource}" />
                <p data-bind="text: Name"></p>
            </div>
        </div>

But for each new element I want to add the jQuery effet like:

        $("#draggable").draggable();

Is there any way to subsribe to event that occures after element added to this list?

like image 481
Eugene Avatar asked Dec 20 '12 15:12

Eugene


1 Answers

The best way to do this is to use custom bindings.

ko.bindingHandlers.draggable= {
    init: function(element, valueAccessor) {
        $(element).draggable();
    }
};

    <div data-bind="foreach: Elements">
        <div data-bind="attr:{id: id}, draggable: {}">
            <img data-bind="attr:{src: ImageSource}" />
            <p data-bind="text: Name"></p>
        </div>
    </div>

Read more about it in the documentation: http://knockoutjs.com/documentation/custom-bindings.html.

like image 179
Artem Vyshniakov Avatar answered Sep 20 '22 17:09

Artem Vyshniakov