Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] seajs config base and etc. #40

Closed
lifesinger opened this issue May 6, 2011 · 3 comments
Closed

[bug] seajs config base and etc. #40

lifesinger opened this issue May 6, 2011 · 3 comments
Milestone

Comments

@lifesinger
Copy link
Member

seajs中的一个bug

当使用base配置时,可以正常加载js,但是use中加载模块有问题,
use的模块无法执行。
目录结构如下:

assets/
  js/
    libs/
      seajs.js
      jquery.js
      mustache.js
      backbone.js
      …
    init.js
    hello.js
  css/

在init.js中初始化程序

seajs.config({
    base : 'assets/js/'
});
seajs.use('hello',function(hello){
    console.log(hello);//null
});

把seajs上移一个目录则没有问题。

module.exports 的一个问题

在其他模块中load时返回的exports对象会有延迟大约50ms,所以
直接操作exports对象回报错,需要delay执行代码,感觉还是直接用require好。

define(function (require, exports, module) {
    module.load(['jquery','backbone'], function ($,Backbone) {
        var Uri = Backbone.Controller.extend({
            //code
        });
        module.exports = new Uri();
    });
});
@lifesinger
Copy link
Member Author

第一个问题,要更改 base 时,建议最好设置为绝对路径,比如 http://a.tbcdn.cn/js/app/xx/
非绝对路径时,和页面地址还有关系,比如:

assets/
  js/
    libs/
      seajs.js
      jquery.js
      ...
    init.js
    hello.js
  css/
test.html

seajs.config({
  base: 'assets/js/'
 });

后,在 test.html 引用时,base 指向 http://your.com/path/to/assets/js/

建议让 base 始终指向 sea.js 所在目录
自己写的模块间,用相对路径引用,这样简单一致。

@lifesinger
Copy link
Member Author

第二个问题,应该用 require,去掉 module.load 即可

假设有一个模块,必须依赖 a 和 b,在某些条件下依赖 c
则应该:

define(function(require, exports, module) {
  var a = require('a');
  var b = require('b');

  if(some_condition) {
    module.load('c', function(c) {
      c.doSomeCoolThing();
    });
  }

 exports.xx = 'xx';
});

module.exports 应该不能放在 module.load 的回调里,因此是同步执行,需要立刻返回的。放在 module.load 里,是异步的,异步回调里,一般不要去修改 module.exports

@jiahao1110
Copy link

有了约定好办事!。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants