今天,騰訊正式開源發佈 Omix 1.0, 讓開發者使用 JSX 或 hyperscript 建立用戶界面。css
靈活的插件體系和豐富的插件生態git
class Hello extends Omi.Component { render() { return <div> Hello {this.data.name}!</div> } } Omi.tag('hello', Hello) class App extends Omi.Component { install() { this.name = 'Omi' } handleClick(e) { this.name = 'Omix' this.update() } style() { return `h3{ color:red; cursor: pointer; }` } render() { return <div> <hello name={this.name}></hello> <h3 onclick={this.handleClick.bind(this)}>Scoped css and event test! click me!</h3> </div> } } Omi.render(new App(), '#container')
const $ = Omi.tags class Hello extends Omi.Component { render() { return $.div( 'Hello' + this.data.name+'!') } } Omi.tag('hello-tag', Hello) class App extends Omi.Component { handleClick(e) { alert(e.target.innerHTML) } render() { return $.div([ $.HelloTag({name: 'Omi'}), $.h3({onclick: this.handleClick}, 'scoped css and event test! click me!') ]) } }
const $ = Omi.tags $.tagName(selector) $.tagName(attrs) $.tagName(children) $.tagName(attrs, children) $.tagName(selector, children) $.tagName(selector, attrs, children)
海外有大量的工程師以爲的 hyperscript 比 JSX 要更加簡潔和方便,可是咱們團隊內部喜歡 JSX 和 hyperscript 一半一半。可是沒有關係 Omix 同時支持兩種方式。下面稍微對比一下二者的使用差別:github
// JSX <ul id="bestest-menu"> {items.map( item => <li class="item" {...attrs(item.id)}>{item.title}</li> )} </ul>
vsnpm
// hyperscript-helpers $.ul('#bestest-menu', items.map( item => $.li('.item', attrs(item.id), item.title)) );
// JSX <MyList>{items.map(item => <MyItem id={item.id} title={item.title} /> )}</MyList>
vs框架
// hyperscript-helpers $.MyList(items.map(item => $.MyItem(item.id, item.title) ))
<MyComponent someProp={{x: 1, y: 2}}/>
vsthis
$.MyComponent({x: 1, y: 2})
Omix 對插件體系進行了升級,使用方便比從前更加簡便,這裏拿 omi-finger 做爲例子, omi-finger 是 Omi的AlloyFinger插件,讓你輕鬆在Omi項目裏支持各類觸摸事件和手勢:spa
npm install omi-finger
import Omi from 'omix'; import 'omi-finger'; class App extends Omi.Component { handleTap(evt){ this.refs.touchArea.innerHTML+='<br/>Tap'; } handleSwipe(evt){ this.refs.touchArea.innerHTML+='<br/>Swipe-'+ evt.direction; } render() { return <div> <div omi-finger ref="touchArea" tap="handleTap" swipe="handleSwipe" > Tap or Swipe Me! </div> </div> } } Omi.render(new App(),"#container");
是否是超級簡便。還在等什麼,用到就是賺到,趕忙開始閱讀 中文文檔 或者在 Omi REPL 把玩一下!插件
This content is released under the MIT License.雙向綁定