我正在使用Requirejs我们的网络应用程序加载JavaScript.问题是我正在将一个undefined对象传递给一个模块,当在其他模块中使用时,该模块被完美地实例化.
好的,这是设置.我的main.js文件requirejs在启动时运行:
require.config({
baseUrl: "/scripts",
paths: {
demographics: "Demographics/demographics",
complaints: "Complaints/complaints",
}
});
require(["templates", "demographics", "complaints", "crossDomain"], function (templates, demographics, complaints) {
"use strict";
console.log("0");
console.log(demographics === undefined);
demographics.View.display();
});
Run Code Online (Sandbox Code Playgroud)
很多配置都被剥离到这个问题中的核心文件.
这是Demographics.js:
define(["ko", "templates", "complaints", "globals", "underscore"], function (ko, templates, complaints, globals) {
// Stuff removed.
return {
View: view
};
});
Run Code Online (Sandbox Code Playgroud)
和 Complaints.js
define([
"demographics",
"ko",
"templates",
"complaints",
"visualeffects",
"globals",
"webservice",
"underscore",
"typewatcher",
"imagesloaded"],
function (demographics, ko, templates, complaints, visualeffects, globals, …Run Code Online (Sandbox Code Playgroud) 我正在研究RequireJS,但我不确定某些事情.
我理解如何加载所有依赖项main.js.但是,我是否需要添加任何逻辑来处理这些依赖项main.js?
对我来说,main.js似乎是一个document.ready状态,你在文档加载时输入逻辑,对吧?
对于其他页面和部分视图,我是否需要创建多个main.js或者我是否可以仅从视图中的依赖项中引用加载的函数<script>?
我一直在尝试使用JSDoc3来生成文件的文档,但是我遇到了一些困难.该文件(Require.js模块)基本上如下所示:
define([], function() {
/*
* @exports mystuff/foo
*/
var foo = {
/**
* @member
*/
bar: {
/**
* @method
*/
baz: function() { /*...*/ }
}
};
return foo;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我无法baz在生成的文档中显示出来.相反,我只获得一个foo/foo模块的文档文件,该文件列出了一个bar成员,但bar没有baz(只是一个foo源代码的链接).
我已经尝试改变bar指令了@property,我已经尝试将baz指令更改为@member或@property,但这些都没有帮助.无论我做什么,巴兹似乎都不想表现出来.
有谁知道我可以用什么指令结构让baz出现在生成的文档中?
PS我试过在JSDoc网站http://usejsdoc.org/howto-commonjs-modules.html上阅读这样的页面,但它只描述了案例foo.bar,而不是foo.bar.baz.
我刚刚开始使用require.js.我已经成功地包装了jquery,一些插件和一些我自己的模块.我正在尝试与Firebug(或Google Chrome的JS控制台)中的模块(或jquery)进行交互,但我运气不错.
从控制台访问这些模块的正确方法是什么?
我正在尝试使用require.js加载一些内容.如果内容不存在,我想捕获错误并通知用户.
在firebug中我可以看到两个错误:
"NetworkError:404 Not Found
......然后几秒钟后:
var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#
Load timeout for modules: modules/messages/messages
http://requirejs.org/docs/errors.html#timeout
Run Code Online (Sandbox Code Playgroud)
我的代码类似于:
require([path], function(content){
//need to catch errors as this will not be called;
});
Run Code Online (Sandbox Code Playgroud)
如何绑定requirejs事件?任何的想法?
我打算使用以下模式来使用基于requireJS的模块作为单例.请注意,classA返回类型为'classA'的实例,而classB,classC和main的其余类返回模块中类的类型.所有这些都是基于MooTools类的类.
我们的想法是将classA用作全局可用的单例,这些方法只是填充程序.如果这是一个可以接受的模式使用的任何想法?
这会在稍后阶段回来咬我吗?我还没试过在项目上运行r.js,所以我有点担心,并寻找一些建议.
// classA.js
define([], function() {
var classA = new Class({
initialize: function (regionId) {
// perform some Initialization.
this.data = null;
},
doSomething: function(param) {
// some thing.
this.data = param;
}
};
return new classA();
});
// classB.js
define(["classA"], function(classA) {
var classB = new Class({
initialize: function (regionId) {
// perform some Initialization.
},
doSomethingElse: function() {
// some thing.
classA.doSomething("Go back to Work Now!");
}
};
return classB; …Run Code Online (Sandbox Code Playgroud) 我想addClass在我的应用程序中使用jQuery UI的功能.
除了我使用正常的jQuery,下划线,主干与requirejs一起分层.
我已经像这样配置了jQuery UI:
require.config({
deps: ["main"],
paths: {
"text": "lib/text"
, "jquery": "lib/jquery"
, "jquery-ui": "lib/jquery-ui"
, "underscore": "lib/underscore"
, "backbone": "lib/backbone"
, "bootstrap": "lib/bootstrap"
, "templates": "../templates"
},
shim: {
"jquery-ui": {
exports: "$",
deps: ['jquery']
},
"underscore": {
exports: "_"
},
"backbone": {
exports: "Backbone",
deps: ["underscore", "jquery"]
},
"bootstrap": ['jquery']
}
});
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中:
define(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
$('div').addClass('white');
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,这只是普通addClass而不是来自jQuery UI的动画.
PS:我使用完整的jQuery版本.
我有一些项目使用RequireJS在浏览器中加载单个JavaScript模块,但我还没有对它们进行优化.在开发和生产中,应用程序为每个JavaScript文件单独发出请求,现在我想使用Grunt修复它.
我试图将一个简单的项目结构组合起来无济于事,所以我想知道是否有人可以为我提供一个有效的例子.我的目标如下:
以下是为了进行此对话的示例结构:
grunt-requirejs-example/
grunt.js
main.js (application entry point)
index.html (references main.js)
lib/ (stuff that main.js depends on)
a.js
b.js
requirejs/
require.js
text.js
build/ (optimized app goes here)
node_modules/ (necessary grunt tasks live here)
Run Code Online (Sandbox Code Playgroud)
具体来说,我正在寻找一个可以从中开始的工作项目结构.我的主要问题是:
grunt.js文件中究竟需要什么,尤其是让r.js优化器工作?watch任务在每次保存文件时自动构建开发模式中的所有内容,那么我全都听见了.我想避免任何减慢循环的事情从进行更改到在浏览器中看到它.我尝试baseUrl在模块中获取Require.js 的配置,但我找不到它存储的位置.
define([], function() {
// Here I'd like to access the `baseUrl` require.js is using
var baseUrl = requirejs.config().baseUrl;
});
Run Code Online (Sandbox Code Playgroud)
在我的例子中,baseUrl由Require.js使用data-main脚本文件的属性设置.
我知道我可以请求module访问config属性(例如define(['module'])),但我找不到如何访问更高级别的配置选项.
我试图了解如何开发独立的Javascript代码.我想编写带有测试和模块的Javscript代码,从命令行运行.所以,我已经安装node.js,并npm与库一起requirejs,underscore和mocha.
我的目录结构如下所示:
> tree .
.
??? node_modules
??? src
? ??? utils.js
??? test
??? utils.js
Run Code Online (Sandbox Code Playgroud)
src/utils.js我正在编写的小模块在哪里,使用以下代码:
> cat src/utils.js
define(['underscore'], function () {
"use strict";
if ('function' !== typeof Object.beget) {
Object.beget = function (o) {
var f = function () {
};
f.prototype = o;
return new f();
};
}
});
Run Code Online (Sandbox Code Playgroud)
并且test/utils.js是测试:
> cat test/utils.js
var requirejs = require('requirejs');
requirejs.config({nodeRequire: require});
requirejs(['../src/utils'], function(utils) { …Run Code Online (Sandbox Code Playgroud)