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

报个 1.3.0 的 bug #419

Closed
undoZen opened this issue Nov 15, 2012 · 6 comments
Closed

报个 1.3.0 的 bug #419

undoZen opened this issue Nov 15, 2012 · 6 comments

Comments

@undoZen
Copy link

undoZen commented Nov 15, 2012

因为我们以前有些 inline 的 script,将全局的依赖 base.js 改成 seajs 的形式之后,要确保运行这些 inline script 之前有加载 preload,所以都改成了

seajs.use([], function () {
    // code depends on base.js
})

1.3.0 的加载方式,只能确保第一个加载 preload,其他的没有。也就是说,第一次 preload() 清除了 config.preload 但还未加载完那些 preload 的文件的时候,如果再有调用 seajs.use 就会出错。

@lifesinger
Copy link
Member

有例子吗?上面的描述太抽象了,我看不懂-.-

@undoZen
Copy link
Author

undoZen commented Nov 17, 2012

https://gist.github.com/4094541

我怀疑如果 seajs.use 不同大小的文件,先 use 大文件时把 preload 清除了,后面再 use 小文件也会遇到同样的问题。还是应该在每次清 preload 前判断一下之前的 preload 有没有加载完成。

@undoZen
Copy link
Author

undoZen commented Nov 17, 2012

另外求教一下玉伯,我们这种情况有没有比 seajs.use([], function () { ... 更好的方式?

是不是最好 define 能返回个什么,然后支持 seajs.use(define(function() { ... })) 这种形式?

@web322
Copy link

web322 commented Nov 17, 2012

(1) 调用第一个use的时候,因为存在preload模块,所以会等待加载完后,在执行use的callback function.
加载了preload模块后,会清除掉preload模块的信息。

(2)调用第二个use的时候,因为不存在preload模块了,同时由因为jquery模块也不符合CMD模块定义,所以很快就执行callback function.

(3)我把alert语句调成了console.log。

2:false //这个先打印
1:true

(4) 加个超时,这下结果都对了
1:true
2:true

//测试代码
seajs.config({ preload: 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' })
seajs.use([], function () {
console.log('1:' + !!window.$)
});

function test(){
seajs.use([], function () {
console.log('2:'+ !!window.$) }
)
}

window.setTimeout("test()", 2000);

其实问题主要是加载不符合模块规范的文件导致的,既然用sea.js ,最好把使用的js模块按照CMD规范包装一下。

@lifesinger
Copy link
Member

针对这个具体问题,建议这么解决:

seajs.config({
  alias: {
     'base': 'path/to/base.js'
  }
})

然后,使用:

seajs.use('base', function() {
   // do sth A
})

seajs.use('base', function() {
   // do sth B
})

上面这种方式应该可以解决问题,而且语义很明确

还有一种方式是:

seajs.config({
  alias: {
     'base': 'path/to/base.js'
  }
})

页面中的 inline 模块可以直接:

define('a', function(require, exports) {
   var base = require('base')

   // do sth A
})

define('b', function(require, exports) {
   var base = require('base')

   // do sth B
})

然后用 use 调用就好:

seajs.use('a')

seajs.use('b')

希望能解决你的问题。

@lifesinger
Copy link
Member

如果想避免每个 inline 模块里都写 require('base'),也可以:

配置:

seajs.config({
  alias: {
    'base': 'path/to/base'
  }
})

模块定义:

define('a', function(require, exports) {
  // do sth A
})

define('b', function(require, exports) {
  // do sth B
})

然后统一调用:

seajs.use('base', function() {

  seajs.use('a')
  seajs.use('b')

})

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

3 participants