简体   繁体   English

与main.js的RequireJS共享数组

[英]RequireJS share array with main.js

Edit: I figured out the solution to my problem. 编辑:我想出了解决我的问题的方法。 There were two issues, first, setting lotInfo in my init function was creating an undefined reference. 有两个问题,首先,在我的init函数中设置lotInfo是创建未定义的引用。 Second, in my lot-plan.js file I wrapped it in "require" but it's needs to be defined as a module so switching require() to define() makes the var accessible. 其次,在我的lot-plan.js文件中,我将其包装在“ require”中,但需要将其定义为模块,因此将require()切换为define()可使var可以访问。

  1. return the desired var in lot-plan.js as the user below suggested. 按照下面的建议,在lot-plan.js中返回所需的var。
  2. Change 更改

    require([ 'raphael' ], function(Raphael){ require(['raphael'],function(Raphael){

to

define([
'raphael'
], function(Raphael){

and remove the reference to lotInfo here 并在此处删除对lotInfo的引用

post_type_archive_plans:{
        init: function(lotInfo){
    // Need the suites var created in lot-plan
        }
    }

Original Post 原始帖子

I'm building a site with requirejs and i'm looking to share an external js file with my main.js file. 我正在使用requirejs构建一个站点,并且希望与main.js文件共享一个外部js文件。 I've separated this code because it's a raphael svg and is just a bunch of gross code I don't want in my main.js. 我已经分离了这段代码,因为它是一个Raphael svg,并且只是我不需要的main.js中的一堆总代码。 What I DO want, in my main.js is the array this file creates. 我想要的是在main.js中创建此文件创建的数组。

FYI this is using a pattern from paul irish 仅供参考,这是使用保罗·爱尔兰的图案

Modified http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/ Only fires on body class (working off strictly WordPress body_class) 修改后的http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/仅在body类上触发(严格执行WordPress body_class)

Require Config 需要配置

require.config({

paths: {
    jquery: [
        '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min',
        '../bower_components/jquery/jquery.min'
    ],
    underscore: [
        '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min',
        '../bower_components/underscore/underscore-min'
    ],
    fastclick: [
        '//cdnjs.cloudflare.com/ajax/libs/fastclick/0.6.10/fastclick.min',
        '../bower_components/fastclick/lib/fastclick'
    ],
    spinjs: [
        '//cdnjs.cloudflare.com/ajax/libs/spin.js/1.3.2/spin.min',
        '../bower_components/spinjs/spin'
    ],
    waitforimages: [
        '../js/lib/jquery.waitforimages.min'
    ],
    backgroundCheck: [
        '../bower_components/backgroundCheck/background-check.min'
    ],
    raphael: [
        '//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min',
        '../bower_components/raphael/raphael-min'
    ],
    swipe: [
        '//cdnjs.cloudflare.com/ajax/libs/swipe/2.0/swipe.min',
        '../bower_components/swipe/swipe'
    ],
    lotInfo: "lot-plan",
    app: 'app'
},
shim: {
    underscore: {
        deps: ['jquery'],
        exports: '_'
    },
    waitforimages: {
        deps: ['jquery']
    },
    app: {
        deps: ['jquery', 'underscore', 'fastclick', 'spinjs', 'waitforimages', 'backgroundCheck', 'raphael', 'swipe', 'lotInfo']
    }
}
});

require([
'jquery',
'fastclick',
'underscore',
'spinjs',
'waitforimages',
'backgroundCheck',
'raphael',
'swipe',
'lotInfo',
'app'
]);

So, all of these files i'm loading are just libraries, but the last file i'm loading, "lotInfo" is the raphael svg file. 因此,我正在加载的所有这些文件只是库,但是我正在加载的最后一个文件“ lotInfo”是raphael svg文件。 Keep in mind everything loads fine and on the proper pages. 请记住,一切正常,并在正确的页面上加载。 Here is the wrapper for the lot-plan.js file, so you can see how it's made. 这是lot-plan.js文件的包装器,因此您可以了解它的制作方法。

require([
'raphael'
], function(Raphael){ if( $('#lot-plan').length ){


// Init
var rsr = Raphael('lot-plan', '452.978', '440.978');

blah blah blah a bunch of code....

var suites = [suite_a4east, suite_a5east, suite_a8east, suite_a9east, suite_a10east, suite_a4west, suite_a9west, suite_a10west, suite_b1east, suite_b2east, suite_b1west, suite_b2west, suite_b3west, suite_b6west, suite_c7, suite_d8, suite_e3, suite_e6, suite_f7, suite_d5];


}// if lot-plan exists
});// require

And you'll see the suites var at the bottom, That is the var I want to access in my app.js file. 而且您会在底部看到套件var,这就是我要在我的app.js文件中访问的var。 This is how I am loading my code per page. 这就是我每页加载代码的方式。

require([
'jquery',
'underscore',
'fastclick',
'spinjs',
'waitforimages',
'backgroundCheck',
'raphael',
'lotInfo',
'swipe'
], function($, _, FastClick, Spinner, waitforimages, BackgroundCheck, Raphael, lotInfo, swipe){
'use strict';


// Modified http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
// Only fires on body class (working off strictly WordPress body_class)

var appname = {

    // All pages
    common: {
        init: function() {
                        //Would execute on all pages
                    }
            }
            post_type_archive_plans:{
        init: function(lotInfo){
    // Need the suites var created in lot-plan
        }
    }

So in post_type_archive_plans is where i need to grab that suites var. 因此,在post_type_archive_plans中,我需要抓住该套件var。 If anybody has any tips for me that would be great! 如果有人对我有任何提示,那就太好了! If you need any more info let me know. 如果您需要更多信息,请告诉我。

Thanks! 谢谢!

Assuming #lot-plan is not dynamically loaded, you're almost there. 假设#lot-plan没有动态加载,您就快到了。 Just return the suites array as part of the lotInfo module. 只需将suites数组作为lotInfo模块的一部分返回lotInfo

In lot-plan.js do: 在lot-plan.js中:

var suites = ...

return { suites: suites }

And in app.js: 在app.js中:

init: function(lotInfo){
    var suites = lotInfo.suites
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM