首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RequireJS共享数组与main.js

RequireJS共享数组与main.js
EN

Stack Overflow用户
提问于 2014-01-21 01:13:52
回答 1查看 149关注 0票数 1

编辑:,我想出了解决问题的方法。有两个问题,首先,在init函数中设置lotInfo是创建一个未定义的引用。其次,在lot-plan.js文件中,我将其包装为"require“,但需要将它定义为一个模块,因此切换require()可以访问var ()。

  1. 按照下面用户的建议,在lot-plan.js中返回所需的var。
  2. 变化 要求( 'raphael‘,函数(Raphael){

代码语言:javascript
运行
复制
define([
'raphael'
], function(Raphael){

并在这里删除对lotInfo的引用

代码语言:javascript
运行
复制
post_type_archive_plans:{
        init: function(lotInfo){
    // Need the suites var created in lot-plan
        }
    }

原邮政

我正在构建一个有需求的站点,我希望与我的main.js文件共享一个外部js文件。我分离了这段代码,因为它是一个raphael,只是一堆我不想在main.js中使用的粗俗代码。我想要的是,在我的main.js中,这个文件创建的数组。

这是使用保罗爱尔兰人的模式

修改后的http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/只在body类上触发(严格地消除WordPress body_class)

要求Config

代码语言:javascript
运行
复制
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'
]);

所以,我正在加载的所有这些文件都只是库,但我加载的最后一个文件"lotInfo“是raphael文件。记住,所有的东西都很好地加载在适当的页面上。下面是lot-plan.js文件的包装器,这样您就可以看到它是如何制作的。

代码语言:javascript
运行
复制
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

您将看到在底部的程序变量,这是我希望在app.js文件中访问的变量。这是我如何加载我的代码每页。

代码语言:javascript
运行
复制
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
        }
    }

因此,在post_type_archive_plans中,我需要获取套件变量。如果有人给我任何提示,那就太好了!如果你需要更多的信息,请告诉我。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-21 16:32:03

假设#lot-plan不是动态加载的,那么您就快到了。只需将suites数组作为lotInfo模块的一部分返回即可。

在lot-plan.js中:

代码语言:javascript
运行
复制
var suites = ...

return { suites: suites }

在app.js中:

代码语言:javascript
运行
复制
init: function(lotInfo){
    var suites = lotInfo.suites
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21247341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档