What to replace elgg.provide() ?

Last updated by Yevgeniy Comments (5)
 
old jscript code doesn't work in 3.0

What to replace elgg.provide() ?

  • Hello Jerome !

    I have read but cannot figure out which module should be used for this code
    //<script>
    elgg.provide('elgg.blogvideocover');
     
    elgg.blogvideocover.init = function() {
    var form = $('form#blog-post-edit');
    var delay = (function(){
    var timer = 0;
    return function(callback, ms) {
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
    };
    })();
    $('input#blog_video_cover_id').keyup(function() {
    delay(elgg.blogvideocover.save, 500 );
    });
    };
     
    elgg.blogvideocover.save = function() {
    var form = $('form#blog-post-edit');
    elgg.action('blogvideocover/save', {
    data: {
    guid: form.find('input[name=guid]').val(),
    url: form.find('input[name=blog_video_cover]').val()
    },
    success: function() {
    elgg.system_message(elgg.echo('blogvideocover:update'));
    }
    });
    };
     
    elgg.register_hook_handler('init', 'system', elgg.blogvideocover.init);
  • Might work. Save the following as /views/default/js/blogvideocover.js

    define(function (require) {
        var $ = require('jquery');
        var elgg = require('elgg');
    
        var BVC = {
            save: function() {
                var form = $('form#blog-post-edit');
                elgg.action('blogvideocover/save', {
                    data: {
                    guid: form.find('input[name=guid]').val(),
                    url: form.find('input[name=blog_video_cover]').val()
                },
                success: function() {
                    elgg.system_message(elgg.echo('blogvideocover:update'));
                }
                });
            },
            init: function() {
                var form = $('form#blog-post-edit');
                var delay = (function() {
                    var timer = 0;
                    return function(callback, ms) {
                        clearTimeout (timer);
                        timer = setTimeout(callback, ms);
                    };
                })();
                $('input#blog_video_cover_id').keyup(function() {
                    delay(elgg.blogvideocover.save, 500 );
                });
            }
        };
        elgg.register_hook_handler('init', 'system', BVC.init);
    }

    and then add in the form view at the beginning

    elgg_require_js('blogvideocover');

    No promise that it works though! The point is: you have to write the AMD module yourself, i.e. rewrite the existing JS code as an AMD module. With the AMD module you also don't need to use elgg_extend_view anymore to load the module because this is done explicitely with elgg_require_js within the view (and only this view) requiring the JS code to be avaiable.

  • iionly thank you very much for your help

  • No guarantee it will work. Might still contain bugs you have to fix. One bug is that you need to change

    elgg.blogvideocover.save

    into

    BVC.save

Navigation