Sha256: 2ce9608be99f9dd60dc8baee4816985fefea978d858857db854479a016e53ab5

Contents?: true

Size: 1.75 KB

Versions: 5

Compression:

Stored size: 1.75 KB

Contents

(function ($) {
    'use strict';

    $.Behave = {};
    $.Behave.sequence = 0;
    $.Behave.loads = [];

    // Refresh one behaviour
    $.Behave.refreshOne = function (behaviour) {
        var element;
        behaviour.ref.find(behaviour.selector).each(function (index) {
            var item = $(this);
            if (item.prop('alreadyBound' + behaviour.key) !== true) {
                behaviour.handler.call(item);
                item.prop('alreadyBound' + behaviour.key, true);
            }
        });
    };

    // Refresh all behaviours
    $.Behave.refresh = function () {
        $.each($.Behave.loads, function (index, behaviour) {
            $.Behave.refreshOne(behaviour);
        });
	console.log("Refresh behaviours");
    };

    // Same API as .on(). Takes in account load events.
    $.fn.behave = function (events, selector, handler) {
        var ref = $(this);
        $.each(events.split(/\s+/ig), function (index, event) {
            var behaviour;
            if (event === "load") {
                behaviour = {ref: ref, selector: selector, handler: handler, key: $.Behave.sequence * 7};
                $.Behave.loads.push(behaviour);
                $.Behave.refreshOne(behaviour);
                $.Behave.sequence += 1;
            } else {
                ref.on(events, selector, handler);
            }
        });
    };
    
    // Retro-compatibility
    $.behave = function (selector, events, handler) {
        $(document).behave(events, selector, handler);
    };

    // Rebinds unbound elements on each ajax request.
    $(document).ajaxComplete($.Behave.refresh);

    // Compatibility with Cocoon and Turbolinks
    $(document).on("cocoon:after-insert page:change", function (event) {
        $.Behave.refresh();
    });



})(jQuery);

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
formize-1.1.0 lib/assets/javascripts/formize-behave.js
formize-1.0.4 lib/assets/javascripts/formize-behave.js
formize-1.0.3 lib/assets/javascripts/formize-behave.js
formize-1.0.2 lib/assets/javascripts/formize-behave.js
formize-1.0.1 lib/assets/javascripts/formize-behave.js