模仿Vuejs的
伪MVVM
库,下面是使用说明
项目地址:https://github.com/miniflycn/Q.jsjavascript
相关项目:https://github.com/miniflycn/Queshtml
模版:vue
<a href="javascript:void(0)" q-text="msg"></a>
脚本:java
var vm = new Q({ el: '#demo', data: { msg: 'hello' } });
则会展现:jquery
<a href="javascript:void(0)">hello</a>
当使用.data方法修改data时候会触发节点数据修改:git
vm.$set('msg', '你好');
则会展现:github
<a href="javascript:void(0)">你好</a>
咱们用Q.js实现了一个TodoMVC的例子:
https://github.com/miniflycn/Q.js/tree/master/examples/todomvcsql
相比jQuery的实现,分层较为清晰,而比BackBone的实现要简单。express
固然咱们与Vuejs的实现很是像,咳咳。数组
告知libaray
如何对节点进行操做,遵循Vuejs写法:
<element prefix-directiveId="[argument:] expression [| filters...]"> </element>
简单例子:
<div q-text="message"></div>
这里表示message
对应的数据,用text
指令进行操做,text
指令是在该节点塞入文字。
directive
举一个咱们在todoMVC的例子:
<input q-todo-focus="editing" />
则表示editing
对应的数据变化时执行todo-focus
指令,看看咱们todo-focus
指令怎么写的:
directives: {
'todo-focus': function (value) { // 若是editing的值为false,则不处理 if (!value) { return; } // 为true则,对该节点focus()一下 var el = this.el; setTimeout(function () { el.focus(); }, 0); } }
directive
目前只提供了极少的通用
directive
,将来可拓展
若是设置了
filter
,则绑定的数据会通过filter
才执行对应的directive
,这是咱们能够在塞入数据前作输出处理,或事件触发前作数据处理。
模版:
<input q-model="msg" q-on="keyup: showMsg | key enter" />
key
是其中一个通用filter,基本实现是:
var keyCodes = { enter : 13, tab : 9, 'delete' : 46, up : 38, left : 37, right : 39, down : 40, esc : 27 }; /** * A special filter that takes a handler function, * wraps it so it only gets triggered on specific * keypresses. v-on only. * * @param {String} key */ function key(handler, key) { if (!handler) return; var code = keyCodes[key]; if (!code) { code = parseInt(key, 10); } return function (e) { if (e.keyCode === code) { return handler.call(this, e); } }; }
则,当keyup发生,keyCode为13(即enter)时候,才会触发showMsg方法。
特制
on
指令会调用的方法,例如:上面讲到的showMsg。
设置方法:
var vm = new Q({ el: '#demo', data: { msg: 'hello' }, methods: { showMsg: function () { alert(this.msg); } } });
则那个input框会在初始化时自动设值为hello,当改变时候msg
值也会改变,当按下回车键
,则会触发showMsg方法打印值。
大部分操做都和对象与数组的操做相同,只有当设置值的时候须要使用
.$set
方法,由于咱们没有defineProperty的支持。
vm.msg
vm.$set('msg', obj);
push
、pop
、unshift
、shift
、indexOf
、splice
、forEach
、filter
感谢@ouvenzhang提供的测试数据,具体参考https://github.com/miniflycn/Q.js/tree/master/benchmarks
用例 | Q.js | jQuery | Native |
---|---|---|---|
单节点html操做OPS | 82,652 ops/sec ±2.32% | 46,526 ops/sec ±5.45% | 1,101,462 ops/sec ±1.06% |
多节点html操做OPS | 23,641 ops/sec ±0.58% | 4,416 ops/sec ±7.76% | 33,434 ops/sec ±1.37% |
1000个节点repeat渲染操做OPS | 13.54 ops/sec ±2.32% | 31.67 ops/sec ±5.45% | 前一个数据为一般的模版引擎 |