Using jQuery plugins in Meteor

扶醉桌前 提交于 2019-12-24 04:37:06

问题


I have been trying to add a jQuery plugin to Meteor but Meteor refuses to let the plugin work client side.

The example is I have this plugin that allows me to shuffle a bunch of divs around called jQuery Shuffle but when I call the following script in the head or from an external file it does not execute. It could be the plugin not publishing functions but it is client side so that makes no sense. I have verified that javascript and jQuery is working through a few console.log() command tests and those seem to work in external files or in the head wether they are inclosed in a jQuery function or not. Here is the code I am trying to use with jQuery Shuffle:

(do take note that this script also is using a few other plugins that appear not to be working either)

$(document).ready(function () {
    var hash = window.location.hash.substr(1);

    // Puts hash into search field
    if (hash !== "") {
        $("#search").val(hash);
        $("#search").focus();
        $('#search').keyup();
    }

    /* initialize shuffle plugin */
    var $grid = $('#grid');
    var $gridn = $('#gridn');

    $grid.shuffle({
        itemSelector: '.item',
        group: 'alll'
    });
    $gridn.shuffle({
        itemSelector: '.item',
        group: 'news'
    });

    /* detects a news post */
    if (hash.indexOf("news") > -1) {
        $('#alll').removeClass('active');
        $('#news').addClass('active');
        $('#n-news').addClass('active');
        $grid.shuffle('shuffle', 'news');
    }

    /* reshuffle when user clicks a filter item */
    $('#filter a').click(function (e) {
        e.preventDefault();
        window.scrollTo(0, 0);

        // set active class
        $('#filter a').removeClass('active');
        $(this).addClass('active');

        // get group name from clicked item
        var groupName = $(this).attr('data-group');

        // reshuffle grid
        $grid.shuffle('shuffle', groupName);
        $gridn.shuffle('shuffle', groupName);

        var n_catagories = ["n-v", "n-am", "n-av", "n-gd", "n-d", "comic", "blog"];
        n_catagories.forEach(function (n_selectedcat) {
            if ($("#" + n_selectedcat).hasClass("active")) {
                $('#news').addClass('active');
                //console.log(n_selectedcat)
            }
        });
    });
    // Sorting options
    $('.select-options').on('change', function () {
        var sort = this.value,
            opts = {};

        // We're given the element wrapped in jQuery
        if (sort === 'date-created') {
            opts = {
                reverse: true,
                by: function ($el) {
                    return $el.data('date-created');
                }
            };
        } else if (sort === 'title') {
            opts = {
                by: function ($el) {
                    return $el.data('title').toLowerCase();
                }
            };
        }

        // Filter elements
        $grid.shuffle('sort', opts);
    });

    // Advanced filtering
    $('.search').on('keyup change', function () {
        var val = this.value.toLowerCase();
        window.scrollTo(0, 0);
        $grid.shuffle('shuffle', function ($el, shuffle) {

            // Only search elements in the current group
            if (shuffle.group !== 'all' && $.inArray(shuffle.group, $el.data('groups')) === -1) {
                return false;
            }

            var text = $.trim($el.find('.item-tags').text()).toLowerCase() + $.trim($el.find('.hidden').text()).toLowerCase();
            return text.indexOf(val) !== -1;
        });
        $gridn.shuffle('shuffle', function ($el, shuffle) {

            // Only search elements in the current group
            if (shuffle.group !== 'all' && $.inArray(shuffle.group, $el.data('groups')) === -1) {
                return false;
            }

            var text = $.trim($el.find('.item-tags').text()).toLowerCase() + $.trim($el.find('.hidden').text()).toLowerCase();
            return text.indexOf(val) !== -1;
        });
    });
    //REMOVE AND HIDE HANDELER
    var $nnitems = $('.nnotice');
    $(".nnotice-button").click(function () {
        Cookies.set('hidennotice', 'true', {
            expires: 31557600
        });
    });
    if (Cookies.get('hidennotice') == 'true') {
        $grid.shuffle('remove', $nnitems);
    }
    $(".nnotice").click(function () {
        $grid.shuffle('remove', $nnitems);
    });
    //Auto Shuffle
    $(".social-toggle").click(function () {
        $(".social-stuffs").slideToggle("slow");
        setTimeout(function () {
            var catagories = ["alll", "v", "am", "av", "gd", "d", "news", "n-v", "n-am", "n-av", "n-gd", "n-d", "comic", "blog"];
            catagories.forEach(function (selectedcat) {
                if ($("#" + selectedcat).hasClass("active")) {
                    $grid.shuffle('shuffle', selectedcat);
                }
            });
        }, 1000);
    });
});

When not enclosed in Meteor, this script works as expected.

I have tried loading the JS files for the plugins by calling them normally through <script type="text/javascript" src="directory/somescript.js"></script> and by loading the scripts through the /client directory meteor uses to process files that are going to the client. It automatically loads these files in a <script> when put here. Even with Meteor loading them it seems not to work. I don't know if it is because the functions inside the plugins need to be published or if it is just no possible to use these kinds of plugins/apis with Meteor.

This is a working, unfinished version of the site without Meteor (and that has missing files and an unfinished color scheme)

Edit:

Basically I just need to be able to load a jQuery plugin somehow and then load the script to control it. That is what I am having trouble doing.


回答1:


Using plugins like query could be a little tricky on meteor, example you are using

$(document).ready(function () {});

And its ok but i prefer to work with

Meteor.startup(function () {

});

Reference here Stack Question about onReady and Startup

Also some people prefer to use the Template.myTemplate.rendered ) function(){} and its works (on my case works), and also try to use some Timeouts

You are not getting anything errors on the console, because nothings wrong happens all DOM elements were created, i face this problems many times using Carrousels, dynamic list from codrops etc.

If you have your web/project on some github repo or working in some cloud host Service like nitrous.io i could help you




回答2:


Safest way is to deliver the plugin as a package and initialise it in template.rendered




回答3:


I had this problem with a jquery plugin. My html code was:

<head>
  <meta charset="utf-8">
  <!-- Plugin JQuery -->
  <script src="js/jquery.easing.min.js"></script>
</head>
<body>
 {{main}}
</body>

Then I put the script after {{main}} template and it works. The script is loaded after all javascript meteor packages:

<head>
  <meta charset="utf-8">
</head>
<body>
 {{main}}
 <!-- Plugin JQuery -->
  <script src="js/jquery.easing.min.js"></script>
</body>


来源:https://stackoverflow.com/questions/27612383/using-jquery-plugins-in-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!