omi/README.md

332 lines
8.6 KiB
Markdown
Raw Normal View History

2018-10-14 07:50:55 +08:00
# Omi 4.0 - 合一
2018-10-13 22:55:38 +08:00
> 下一代 Web 框架,去万物糟粕,合精华为一。
2018-05-03 10:46:34 +08:00
2018-10-13 22:56:53 +08:00
### 特性
2018-05-03 10:46:34 +08:00
2018-10-13 22:55:38 +08:00
- 0.5 KB 的代码尺寸,比小更小
2018-10-14 07:59:35 +08:00
- 顺势而为,顺从浏览器的发展和 API 设计
2018-10-13 22:55:38 +08:00
- Webcomponents + JSX 相互融合为一个框架 Omi
2018-10-14 21:39:17 +08:00
- JSX 是开发体验最棒(智能提示)、[语法噪音最少](https://github.com/facebook/jsx#why-not-template-literals)的 UI 表达式
2018-10-13 22:55:38 +08:00
- 每一个组件拥有 update 方法自由渲染最佳更新视图的时机,功耗低,自由度高,性能卓越
- 局部 CSS 最佳解决方案(Shadow DOM),社区为局部 CSS 折腾了不少框架Shadow DOM Style 是最完美的方案
2018-10-14 21:41:20 +08:00
- 类似 WeStore 体系99.9% 的项目不需要什么时间旅行,也不仅仅 redux 能时间旅行,请不要上来就 reduxOmi store 体系可以满足所有项目
2018-10-14 07:58:13 +08:00
- 看看[Facebook React 和 Web Components对比优势](https://www.cnblogs.com/rubylouvre/p/4072979.html)Omi 融合了各自的优点,而且给开发者自由的选择喜爱的方式
2017-03-09 16:26:06 +08:00
2018-10-14 14:05:06 +08:00
对比同样开发 TodoApp Omi 和 React 渲染完的 DOM 结构:
2018-10-14 14:02:19 +08:00
2018-10-14 14:05:06 +08:00
![](./assets/omi-render.jpg) ![](./assets/react-render.jpg)
2018-10-14 16:39:50 +08:00
左边是Omi右边是 ReactOmi 使用 Shadow DOM 隔离样式和语义化结构。
2018-10-14 14:02:19 +08:00
2018-05-03 10:46:34 +08:00
---
2017-03-23 07:53:17 +08:00
2018-05-03 10:46:34 +08:00
- [Getting Started](#getting-started)
- [Hello Omi](#hello-omi)
2018-10-14 21:33:46 +08:00
- [TodoApp](#todoapp)
- [Store](#store)
2018-05-03 10:46:34 +08:00
- [Lifecycle](#lifecycle)
- [Install](#install)
- [Links](#links)
- [License](#license)
## Getting Started
### Hello Omi
2017-03-23 07:53:17 +08:00
2018-10-13 22:55:38 +08:00
先创建一个自定义元素:
2018-02-25 16:06:09 +08:00
2018-10-13 22:55:38 +08:00
```js
2018-10-14 17:22:38 +08:00
import { WeElement, define } from 'omi'
2018-05-03 10:46:34 +08:00
2018-10-13 22:55:38 +08:00
class HelloElement extends WeElement {
2018-10-14 07:50:55 +08:00
static get props(){
2018-10-14 09:24:19 +08:00
//如果不写入就无法监听到属性变更且自动刷新视图
return ['prop-from-parent', 'msg']
2018-02-25 16:06:09 +08:00
}
2018-10-13 22:55:38 +08:00
onClick = (evt) => {
2018-10-14 07:50:55 +08:00
//trigger CustomEvent
this.fire('abc', { name : 'dntzhang', age: 12 })
2018-10-13 22:55:38 +08:00
evt.stopPropagation()
2018-02-25 16:06:09 +08:00
}
2018-10-13 22:55:38 +08:00
css() {
return `
div{
color: red;
2018-10-14 07:50:55 +08:00
cursor: pointer;
2018-10-13 22:55:38 +08:00
}`
2018-02-25 16:06:09 +08:00
}
2018-10-14 07:50:55 +08:00
render(props) {
2018-10-13 22:55:38 +08:00
return (
<div onClick={this.onClick}>
2018-10-14 07:50:55 +08:00
Hello {props.msg} {props.propFromParent}
<div>Click Me!</div>
2018-10-13 22:55:38 +08:00
</div>
)
}
}
2018-10-14 17:22:38 +08:00
define('hello-element', HelloElement)
2018-10-13 22:55:38 +08:00
```
使用该元素:
``` js
2018-10-14 17:22:38 +08:00
import { render, WeElement, define } from 'omi'
2018-10-13 22:55:38 +08:00
import './hello-element'
class MyApp extends WeElement {
2018-10-14 17:06:50 +08:00
static get data() {
return { abc: '', passToChild: '' }
}
2018-10-14 07:50:55 +08:00
2018-10-14 17:06:50 +08:00
//bind CustomEvent
2018-10-14 07:50:55 +08:00
onAbc = (evt) => {
2018-10-14 17:06:50 +08:00
// get evt data by evt.detail
2018-10-14 07:50:55 +08:00
this.data.abc = ' by ' + evt.detail.name
this.update()
}
2018-10-13 22:55:38 +08:00
css() {
return `
div{
color: green;
}`
2018-09-14 14:55:26 +08:00
}
2018-10-13 22:55:38 +08:00
2018-10-14 07:50:55 +08:00
render(props, data) {
2018-05-03 10:46:34 +08:00
return (
2018-10-14 14:02:19 +08:00
<div>
2018-10-14 07:50:55 +08:00
Hello {props.name} {data.abc}
<hello-element onAbc={this.onAbc} prop-from-parent={data.passToChild} msg="WeElement"></hello-element>
2018-10-13 22:55:38 +08:00
</div>
)
2018-02-25 16:06:09 +08:00
}
}
2017-03-23 07:53:17 +08:00
2018-10-14 17:22:38 +08:00
define('my-app', MyApp)
2018-10-13 22:55:38 +08:00
2018-10-14 07:50:55 +08:00
render(<my-app name='Omi v4.0'></my-app>, 'body')
2018-10-13 22:55:38 +08:00
```
2018-02-25 16:06:09 +08:00
2018-10-14 09:15:21 +08:00
告诉 Babel 把 JSX 转化成 Omi.h() 的调用:
2018-02-25 16:06:09 +08:00
2018-05-03 10:46:34 +08:00
``` json
{
"presets": ["env", "omi"]
2018-02-25 16:06:09 +08:00
}
2018-05-03 10:46:34 +08:00
```
2018-02-25 16:06:09 +08:00
2018-10-13 22:55:38 +08:00
需要安装下面两个 npm 包支持上面的配置:
2018-02-25 16:06:09 +08:00
2018-05-03 10:46:34 +08:00
``` bash
"babel-preset-env": "^1.6.0",
"babel-preset-omi": "^0.1.1",
```
2018-10-14 17:06:50 +08:00
如果不想把 css 写在 js 里,你可以使用 [to-string-loader](https://www.npmjs.com/package/to-string-loader), 比如下面配置:
2018-02-25 16:06:09 +08:00
2018-05-03 10:46:34 +08:00
``` js
2018-05-09 10:18:53 +08:00
{
test: /[\\|\/]_[\S]*\.css$/,
use: [
'to-string-loader',
'css-loader'
]
}
2018-05-03 10:46:34 +08:00
```
2018-10-13 22:55:38 +08:00
如果你的 css 文件以 `_` 开头, css 会使用 to-string-loader. 如:
2018-05-09 10:18:53 +08:00
``` js
2018-10-14 17:22:38 +08:00
import { render, WeElement, define } from 'omi'
2018-10-13 22:55:38 +08:00
//typeof cssStr is string
import cssStr from './_index.css'
2018-05-09 10:18:53 +08:00
2018-10-13 22:55:38 +08:00
class MyApp extends WeElement {
2018-05-09 10:18:53 +08:00
2018-10-13 22:55:38 +08:00
css() {
return cssStr
2018-05-09 10:18:53 +08:00
}
2018-10-14 07:50:55 +08:00
...
...
...
2018-05-09 10:18:53 +08:00
```
2018-10-14 13:47:23 +08:00
### TodoApp
下面列举一个相对完整的 TodoApp 的例子:
```js
2018-10-14 17:22:38 +08:00
import { render, WeElement, define } from 'omi'
2018-10-14 13:47:23 +08:00
class TodoList extends WeElement {
render(props) {
return (
<ul>
{props.items.map(item => (
<li key={item.id}>{item.text}</li>
))}
</ul>
);
}
}
2018-10-14 17:22:38 +08:00
define('todo-list', TodoList)
2018-10-14 13:47:23 +08:00
class TodoApp extends WeElement {
2018-10-14 17:06:50 +08:00
static get data() {
return { items: [], text: '' }
2018-10-14 13:47:23 +08:00
}
render() {
return (
<div>
<h3>TODO</h3>
<todo-list items={this.data.items} />
<form onSubmit={this.handleSubmit}>
<input
id="new-todo"
onChange={this.handleChange}
value={this.data.text}
/>
<button>
Add #{this.data.items.length + 1}
</button>
</form>
</div>
);
}
handleChange = (e) => {
this.data.text = e.target.value
}
handleSubmit = (e) => {
e.preventDefault();
if (!this.data.text.length) {
return;
}
this.data.items.push({
text: this.data.text,
id: Date.now()
})
this.data.text = ''
this.update()
}
}
2018-10-14 17:22:38 +08:00
define('todo-app', TodoApp)
2018-10-14 13:47:23 +08:00
render(<todo-app></todo-app>, 'body')
```
2018-05-03 10:46:34 +08:00
2018-10-14 21:33:46 +08:00
### Store
2018-10-14 21:57:20 +08:00
强大的 Store 体系是高性能的原因,除了靠 props 决定组件状态的组件,其余组件所有 data 都挂载在 store 上:
2018-10-14 21:33:46 +08:00
```js
export default {
data: {
items: [],
text: '',
firstName: 'dnt',
lastName: 'zhang',
fullName: function () {
return this.firstName + this.lastName
},
globalPropTest: 'abc', //更改我会刷新所有页面,不需要再组件和页面声明data依赖
ccc: { ddd: 1 } //更改我会刷新所有页面,不需要再组件和页面声明data依赖
},
add: function () {
this.data.items.push({
text: this.data.text,
id: Date.now()
})
this.data.text = ''
this.update()
},
globalData: ['globalPropTest', 'ccc.ddd'],
logMotto: function () {
console.log(this.data.motto)
},
//默认 false为 true 会无脑更新所有实例
//updateAll: true
}
```
自定义 Element 需要声明依赖的 data这样 Omi store 根据自定义组件上声明的 data 计算依赖 path 并会按需局部更新。如:
```js
class TodoApp extends WeElement {
static get data() {
return { items: [], text: '' }
}
...
...
...
handleChange = (e) => {
this.store.data.text = e.target.value
}
handleSubmit = (e) => {
e.preventDefault();
if (!this.store.data.text.length) {
return;
}
this.store.add()
}
}
define('todo-app', TodoApp)
```
需要在 render 的时候从根节点注入 store 才能在所有自定义 Element 里使用 this.store:
```js
render(<todo-app></todo-app>, 'body', store)
```
2018-10-14 21:57:20 +08:00
总结一下:
* store.data 用来列出所有属性和默认值(除去 props 决定的视图的组件)
* 组件和页面的 data 用来列出依赖的 store.data 的属性 (omi会记录path),按需更新
* 如果页面简单组件很少,可以 updateAll 设置成 true并且组件和页面不需要声明 data也就不会按需更新
* globalData 里声明的 path只要修改了对应 path 的值就会刷新所有页面和组件globalData 可以用来列出所有页面或大部分公共的属性 Path
2018-05-03 10:46:34 +08:00
### Lifecycle
2018-02-25 16:06:09 +08:00
2018-05-03 10:46:34 +08:00
| Lifecycle method | When it gets called |
|-------------------------------|--------------------------------------------------|
2018-10-13 22:55:38 +08:00
| `install` | before the component gets mounted to the DOM |
| `installed` | after the component gets mounted to the DOM |
| `uninstall`     | prior to removal from the DOM                   |
| `beforeUpdate` | before `render()` |
| `afterUpdate` | after `render()` |
2017-03-23 07:53:17 +08:00
2018-02-25 16:06:09 +08:00
## Install
2017-03-23 07:53:17 +08:00
2018-10-14 07:50:55 +08:00
Get javascript file from Github:
2017-03-23 07:53:17 +08:00
2018-10-14 07:45:53 +08:00
* [omi/master/dist](https://github.com/Tencent/omi/tree/master/dist)
* [omi.js](https://github.com/Tencent/omi/blob/master/dist/omi.js)
2018-10-14 21:43:57 +08:00
* [omi.min.js](https://github.com/Tencent/omi/blob/master/dist/omi.min.js)
2017-03-23 07:53:17 +08:00
2018-05-03 10:46:34 +08:00
## Links
2017-03-23 07:53:17 +08:00
2018-10-14 07:45:53 +08:00
- [westore](https://github.com/dntzhang/westore)
2018-05-03 11:19:37 +08:00
- [omijs.org](http://omijs.org/)
2017-03-23 07:53:17 +08:00
2018-05-03 10:46:34 +08:00
## License
2017-03-09 16:26:06 +08:00
2018-10-14 08:23:31 +08:00
MIT © Tencent
Please contact me[@dntzhang](https://github.com/dntzhang) for any questions.