一个复杂网站或单页应用的实际例子
组件容器就是包含所有组件的一个页面。
Transformers 框架是一个以 Javascript 为控制层,鼓励前端渲染的,专注组件化这一件事情的前端框架。
框架诞生于 2009 年
解耦,解耦,解耦,重要的事情说三遍!
<!DOCTYPE html>
<html>
<body>
<aside>
<tf:common-navigation></tf:common-navigation>
</aside>
<div>
<tf:home-info></tf:home-info>
<tf:home-history></tf:home-history>
<tf:home-message></tf:home-message>
</div>
</body>
</html>
// 定义名为 MyInboxMessage 的组件
TF.define('MyInboxMessage', {
DomReady: function() {
},
// Action 是组件对外的接口
testAction: function(args) {
},
// 组件私有方法,外部无法访问
renderOk: function() {
}
});
组件名: MyInboxMessage (映射关系是可配置的)
JS: http://domain.com/my/inbox/message.js
HTML: http://domain.com/my/inbox/message.html
DATA: http://domain.com/my/inbox/message.php
↑ 上面的组件名和 URL 的映射关系是可以自定义的 ↑
TF.define('EarthAnimal', {
});
TF.define('EarthCat', {
Mentor: {
name: 'EarthAnimal'
}
});
TF.define('EarthWhiteCat', {
Mentor: {
name: 'EarthCat'
}
});
在使用组件的时候,如果指明异步加载,那么在组件没有被操作的时候,不会加载组件的任何资源。
<!DOCTYPE html>
<html>
<body>
<aside>
<tf:common-navigation></tf:common-navigation>
</aside>
<div>
<tf:home-info></tf:home-info>
<tf:home-history async></tf:home-history>
<tf:home-message async></tf:home-message>
</div>
</body>
</html>
为了方便的使用第三方开发的组件,框架提供了组件 Namespace 的功能。
<!DOCTYPE html>
<html>
<body>
<tf:google:my-inbox-message></tf:google:my-inbox-message>
</body>
</html>
在需要记录用户操作历史的页面,可以使用组件级路由
http://domain.com/mail#my-inbox-message/参数1/参数2.../参数n
HULK 私有云项目
此项目是 360 内部私有云管理平台,使用 Transformers 框架开发,共计开发组件 400 多个,框架可以很好的管理这些组件。