jeff
jeff

Reputation: 1197

How to "inject" a variable into an anonymous function

I have a following jQuery (ui) code:

    function initSortable() {
    var placeholderClass = "ui-state-highlight";
    var movement; /* = {'id': 0, 'from': 0, 'to': 0};*/
    setPlaceholderClassHeight(placeholderClass);
    $(".listTable td").each(function () {
        $(this).css("width", $(this).width());
    });
    $('.listTable tbody').sortable({
        placeholder: placeholderClass,
        forcePlaceholderSize: true,
        axis: "y",
        cursor: "grab",
        opacity: 0.8,
        start: function (event, ui) {
            ui.item.toggleClass("highlight");
            movement.id = ui.item.data('id');
            movement.from = ui.item.data('id');
            console.log(ui.item.index());
            console.log(ui.item.data('id'));
        },
        stop: function (event, ui) {
            ui.item.toggleClass("highlight");
        },
        update: function (event, ui) {
            console.log(ui.item.index());
            movement.to = ui.item.index();
            console.log(movement);
        }
    });
}

it doesn't work as movement is unknown in functions assigned to start/stop/update. How can I use and change the movement variable?

Upvotes: 0

Views: 382

Answers (1)

Vigneswaran Marimuthu
Vigneswaran Marimuthu

Reputation: 2532

movement is declared in right place and it should update. It is not updating because it is undefined. Declare var movement = {} and check

Upvotes: 3

Related Questions