Introduction

Trunk.js把前端开发划分成view(页面最终呈现,事件交互)、model(view背后逻辑、数据处理)和collection(模型集合),view和model一一对应作为一个微观世界。view因业务需求可能变得很复杂,所以支持嵌套,即一个微观世界里可能囊括多个微观世界。随着越来越多的微观世界产生,那些通用的逻辑、操作、现象就可以抽象出一个抽象的世界(本身也可能是nested)。到最后,前端开发就像搭积木一样简单而又清晰。

component
[Not supported by viewer]
view
[Not supported by viewer]
view
[Not supported by viewer]
model
[Not supported by viewer]
Server
[Not supported by viewer]
fetch
[Not supported by viewer]
json
[Not supported by viewer]
model
[Not supported by viewer]
view
[Not supported by viewer]
collection
[Not supported by viewer]
DOM
[Not supported by viewer]
extend
[Not supported by viewer]
view
[Not supported by viewer]
model
[Not supported by viewer]
extend
[Not supported by viewer]
click
[Not supported by viewer]
render
[Not supported by viewer]
listen
[Not supported by viewer]
listen
[Not supported by viewer]
listen
[Not supported by viewer]
listen
[Not supported by viewer]

如上图,一一对应的view和model构成了一个组件(component),组件的最大作用就是提高代码的可复用性,组件本身可以直接实例化或者被其他view、model继承(严格意义上Trunk和Trunk.Model是一个最基础的组件)。

跨模块通信由事件驱动,典型的观察者模式。

// Register event
view1.listen(model2, 'save', function() {
// Do something
})
// Fire event
model2.trigger('save')

How to use

Trunk.js依赖 jQuery

方式1:原始模式

<script src="lib/jquery.js"></script>
<script src="lib/Trunk.js"></script>
<script src="js/page.js"></script>

page.js

$(function() {
var app = new Trunk({
el: 'body'
})
// Do...
})

方式2:AMD规范,如 requirejs

<script src="lib/require.min.js"></script>
<script src="js/require.config.js"></script>
<script src="js/page.js"></script>

require.config.js

require.config({
paths: {
'jquery': 'lib/jquery',
'Trunk': 'lib/Trunk'
}
})

page.js

require(['Trunk'], function(Trunk) {
var app = new Trunk({
el: 'body'
})
// Do...
})

方式3:CommonJS规范,如 webpack (待完善)

Download

Trunk.zip