aralejs / aralejs.github.io

开放、简单、易用的前端基础类库
http://aralejs.github.io
MIT License
1.37k stars 333 forks source link

为什么使用 arale 的模块时要用 use 而不是 require 呢 #325

Closed hbrls closed 10 years ago

hbrls commented 10 years ago

我的理解是 use 是全局性的,一直要用的,比如 gallery 类的 jquery, underscore 这种。而实例化一个模块的时候用 require 就近不是更清晰吗?

popomore commented 10 years ago

在 cmd 模块内部使用 require,其他使用 use,一般这么用。

hbrls commented 10 years ago

那么就会出现这样的代码

seajs.use([
  './a',
  './b'
  ...
], function (A, B, ...) {
  // biz
});

如果在一个页面上使用很多模块,这个 ... 就会很长,在维护代码比如merge的时候,很容易弄错,比如次序就很难一眼数清楚。

我记得以前 @lifesinger 说过 requirejs 推荐的这种写法他不喜欢,为什么这里要这样用呢,或者只是个习惯问题?

lifesinger commented 10 years ago

use 一般参数不会很长,用来启动初始模块。如果初始模块很长的话,要考虑是否合理。

2013/12/13 Albert notifications@github.com

那么就会出现这样的代码

seajs.use([ './a', './b' ... ], function (A, B, ...) { // biz });

如果在一个页面上使用很多模块,这个 ... 就会很长,在维护代码比如merge的时候,很容易弄错,比如次序就很难一眼数清楚。

我记得以前 @lifesinger https://github.com/lifesinger 说过 requirejs 推荐的这种写法他不喜欢,为什么这里要这样用呢,或者只是个习惯问题?

— Reply to this email directly or view it on GitHubhttps://github.com/aralejs/aralejs.org/issues/325#issuecomment-30438398 .

王保平 / 玉伯(射雕) 送人玫瑰手有余香

hbrls commented 10 years ago

比如首页上,可能要用到 Sticky, Switchable:Accordion, Switchable:Carousel, Calendar, Autocomplete ,这些都是很合理的需求吧,假如再用用 Cookie, Popup, Tip,一下子就很长了。有没有上线的使用多个模块的例子给看看呢?

edokeh commented 10 years ago

你可以写一个服务首页的 CMD 模块,在模块中 require 这些组件,在页面上 use 这个模块

hbrls commented 10 years ago

@edokeh 我现在改成这样了,是这个意思吗

<script id="seajsnode" src="/static/js/sea-modules/seajs/seajs/2.1.1/sea.js"></script>
<script src="/static/js/seajs_config.js"></script>
<script src="/static/js/main.js"></script>

# main.js
seajs.use(['js/biz']);

# biz.js
define(function (require, exports, module) {
  var $ = require('$');
  var Tabs = require('arale/switchable/1.0.1/tabs');

  tabs = new Tabs({ ... });
});
edokeh commented 10 years ago

是的,看你怎么理解了,首页所需要的 JS 也是一个模块,道理上说得通

如果你不想额外写一个首页模块,那就只能在页面里面 use 多个组件了,其实你可以分开来写,就没那么冗长了 比如在用到 Tab 组件的 HTML 下面写 use('tab') ,在日历相关的 HTML 下面写 use('Calendar') 这样当你要去掉页面某个局部的时候,也不会漏掉对应的 JS

hbrls commented 10 years ago

哦,那看起来问题是 html 也应该模块化,这个我以前都不做的,所以才有这种问题。多谢啦

hbrls commented 10 years ago

终于明白这篇在说什么了 《代码组织实践》

popomore commented 10 years ago

挖到一篇

https://github.com/zjcqoo/mod/issues/1#issuecomment-22772570

hbrls commented 10 years ago

恩,这个清楚多了,多谢