【问题标题】:Calling function outside file在文件外调用函数
【发布时间】:2014-11-11 06:48:59
【问题描述】:

我试图在主 js 上的 JSfile 之外调用这个函数,但控制台抛出了

Uncaught ReferenceError: Boxlayout is not defined main.js?fc8b2171b5ceebf37d7deb392265939f71c1a998:6(匿名函数)

这是我的 boxlayout.js

boxlayout.js

Boxlayout = (function () {
    var $el = $('#bl-main'),
        $sections = $el.children('section'),
        // works section
        $sectionWork = $('#bl-work-section'),
        // work items
        $workItems = $('#bl-work-items > li'),
        // work panels
        $workPanelsContainer = $('#bl-panel-work-items'),
        $workPanels = $workPanelsContainer.children('div'),
        totalWorkPanels = $workPanels.length,
        // navigating the work panels
        $nextWorkItem = $workPanelsContainer.find('nav > span.bl-next-work'),
        // if currently navigating the work items
        isAnimating = false,
        // close work panel trigger
        $closeWorkItem = $workPanelsContainer.find('nav > span.bl-icon-close'),
        transEndEventNames = {
            'WebkitTransition': 'webkitTransitionEnd',
                'MozTransition': 'transitionend',
                'OTransition': 'oTransitionEnd',
                'msTransition': 'MSTransitionEnd',
                'transition': 'transitionend'
        },
        // transition end event name
        transEndEventName = transEndEventNames[Modernizr.prefixed('transition')],
        // support css transitions
        supportTransitions = Modernizr.csstransitions;

    function init() {
        initEvents();
    }

    function initEvents() {

        $sections.each(function () {

            var $section = $(this);

            // expand the clicked section and scale down the others
            $section.on('click', function () {

                if (!$section.data('open')) {
                    $section.data('open', true).addClass('bl-expand bl-expand-top');
                    $el.addClass('bl-expand-item');
                }

            }).find('span.bl-icon-close').on('click', function () {

                // close the expanded section and scale up the others
                $section.data('open', false).removeClass('bl-expand').on(transEndEventName, function (event) {
                    if (!$(event.target).is('section')) return false;
                    $(this).off(transEndEventName).removeClass('bl-expand-top');
                });

                if (!supportTransitions) {
                    $section.removeClass('bl-expand-top');
                }

                $el.removeClass('bl-expand-item');

                return false;

            });

        });

        // clicking on a work item: the current section scales down and the respective work panel slides up
        $workItems.on('click', function (event) {

            // scale down main section
            $sectionWork.addClass('bl-scale-down');

            // show panel for this work item
            $workPanelsContainer.addClass('bl-panel-items-show');

            var $panel = $workPanelsContainer.find("[data-panel='" + $(this).data('panel') + "']");
            currentWorkPanel = $panel.index();
            $panel.addClass('bl-show-work');

            return false;

        });

        // navigating the work items: current work panel scales down and the next work panel slides up
        $nextWorkItem.on('click', function (event) {

            if (isAnimating) {
                return false;
            }
            isAnimating = true;

            var $currentPanel = $workPanels.eq(currentWorkPanel);
            currentWorkPanel = currentWorkPanel < totalWorkPanels - 1 ? currentWorkPanel + 1 : 0;
            var $nextPanel = $workPanels.eq(currentWorkPanel);

            $currentPanel.removeClass('bl-show-work').addClass('bl-hide-current-work').on(transEndEventName, function (event) {
                if (!$(event.target).is('div')) return false;
                $(this).off(transEndEventName).removeClass('bl-hide-current-work');
                isAnimating = false;
            });

            if (!supportTransitions) {
                $currentPanel.removeClass('bl-hide-current-work');
                isAnimating = false;
            }

            $nextPanel.addClass('bl-show-work');

            return false;

        });

        // clicking the work panels close button: the current work panel slides down and the section scales up again
        $closeWorkItem.on('click', function (event) {

            // scale up main section
            $sectionWork.removeClass('bl-scale-down');
            $workPanelsContainer.removeClass('bl-panel-items-show');
            $workPanels.eq(currentWorkPanel).removeClass('bl-show-work');

            return false;

        });

    }

    return {
        init: init
    };

})();

我正在尝试使用

ma​​in.js

Meteor.startup(function(){
  Boxlayout.init();

});

此代码在其他应用程序上的流星之外工作,我也使用来自 JQuery 的$.(function(){});,但现在我在流星上工作,这只是行不通,我阅读了一些文档和堆栈溢出问题,所以我找到了这个并尝试

Older stack overflow Question

【问题讨论】:

    标签: javascript jquery meteor


    【解决方案1】:

    因为您的函数包含在括号中并立即使用() 调用,所以您实际上将Boxlayout 指定为匿名IIFE(立即调用函数表达式)的结果,但该函数当前不返回任何内容,所以Boxlayout 设置为 undefined(而不是函数本身)。您首先需要来自 IIFE 的return this

    例如在函数末尾添加以下代码:

    return this;
    

    您可以将其构建为类实例,并将其init 属性设置为“构造函数”中的函数(为清楚起见,删除了所有其余代码):

    Boxlayout = (function () {
    
        // Set an init property of this instance to be a function 
        this.init = function () {
            alert("init")
        }
    
        // return the instance of this anonymous class
        return this;
    })();
    
    // Then to use it later
    $(function () {
        // Call the init method on the specific single instance we created
        Boxlayout.init();
    });
    

    JSFiddle: http://jsfiddle.net/hzz9jqjc/1/

    【讨论】:

    • @Etjana:示例只是原始 JS(测试行除外)。您是说工作的 JSFiddle 对您不起作用,还是您无法将它应用到您的代码中? “那行不通”并没有足够的信息继续下去:)
    • 兄弟,答案真的很好,它的工作很抱歉“不工作”,你知道这正是我想要做的,同样的例子就是这个包,atmospherejs.com/jelena/meteor-page-transitionsm 好像有人已经有同样的问题,但知道它似乎包不工作
    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多