ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 1 qvm意义 >[info] qvm是一个适用于移动端的迷你mvvm框架。参考vuejs的设计,进行简化。 >[info] 使用zepto作为基本依赖库,浏览器的原生支持基础上使用最少代码实现一个vvm框架 ## 2 vm模型结构 ~~~ $qvm = { $id: 823832887973495 ;qvm对象的全局id。 $elem: #div ;View视图层节点 $model:{ ;viewmodel 通过指令directive关联view到model的操作 text:{ ns:ns, ;对象或者指令的namespace assessor:asccessor, ;同一的外部访问器,vm通过修改它改变 key:text, ;指令名称 directive:[directive], ;指令集 setter:function, ;对象设置方法 getter:function, ;对象获取值方法 tpl:#div ;模板字符串 } }, data:{ ;model的数据层 } } ~~~ ## 3 q-text修改节点innertHTML ~~~ <div id="demo" q-text='text'></div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> var vm = qvm.get({ selector:'#demo', data:{ text:'<h2>hello world!</h2>' } }); setTimeout(function(){ vm.text.accessor = '<h2>Fuck world!</h2>'; },4000) </script> ~~~ ## 4 q-class修改节点class ~~~ <style> .red{ color: red; } .green{ color: green; } </style> <div id="demo" q-text='text' q-class="color"></div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> var vm =qvm.get({ selector:'#demo', data:{ text:'<h2>Hello world!</h2>' color:'red' } }); setTimeout(function(){ vm.text.accessor = '<h2>Fuck World!</h2>' vm.class.accessor = 'green'; },4000); </script> ~~~ ## 5 q-attr修改属性 ~~~ <div><a><img id="demo" q-attr-src="img" width="50" height="30"></a></div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> var vm = qvm.get({ selector: '#demo', data:{ img: 'http://9.url.cn/edu/banner/img/505d9c39_760_300.jpg' } }); setTimeout(function(){ vm.attr_src.accessor = 'http://9.url.cn/edu/banner/img/880facff_760_300.jpg'; },4000) </script> ~~~ ## 6 q-data修改数据值 ~~~ <div id="demo" q-data-title="text"><a><img q-attr-src="img" width="50" height="30"></a></div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> var vm = qvm.get({ selector:'#demo', data:{ text: 'Ps 大神教程', img: 'http://9.url.cn/edu/banner/img/505d9c39_760_300.jpg' } }); setTimeout(function(){ vm.data_title.accessor = '艺术人生'; },4000) </script> ~~~ ## 7 q-repeat嵌套使用 ~~~ <style> .red{ color: red; } .green{ color: green; } </style> <div id="demo" q-class="color" q-repeat="list"> <div> <img q-attr-src="img" width="50" height="30"><span q-text="title" q-class="color"></span> </div> </div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> var list =[{ img: 'http://9.url.cn/edu/banner/img/10b0af94_760_300.jpg', title: '音乐改变世界' },{ img: 'http://9.url.cn/edu/banner/img/880facff_760_300.jpg', title: 'PS 大神教程' },{ img: 'http://9.url.cn/edu/banner/img/505d9c39_760_300.jpg', title: '艺术人生' }]; var vm = qvm.get({ selector: '#demo', data:{ text: '<h2>Hello World!</h2>', color: 'red', list: list } }); setTimeout(function(){ delete list[2]; list[0].title='music change the world'; list[1].title='PS master learning'; vm.class.accessor = 'green'; vm.repeat.accessor = list; },4000); </script> ~~~ ## 8 q-对象内部渲染 ~~~ <style> .red{ color: red; } .green{ color: green; } </style> <div id="demo" q-repeat="list"> <div> <span q-class="color" q-text="title"></span> <span> <img q-attr-src="img.src" width="50" height="30"> </span> <div> </div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> ;data属性 var data = { title: '<h2>Hello World!</h2>', color: 'red', img: { src: 'http://9.url.cn/edu/banner/img/880facff_760_300.jpg' } }; ;vm var vm = qvm.get({ selector: '#demo', data: { list: data } }); ;数据层操作 setTimeout(function(){ data.color = 'green'; data.title = '<h2>Fuck World!</h2>'; data.img.src = 'http://9.url.cn/edu/banner/img/10b0af94_760_300.jpg'; vm.repeat.accessor = data; },4000); </script> ~~~ ## 9 q-on简单事件绑定 ~~~ <style> .red{ color: red; } .green{ color: green; } </style> <div id="demo" q-repeat="list" q-on="click|action"> <div> <span q-class="color" q-text="title"></span> <span> <img q-attr-src="img.src" width="50" height="30"> </span> <div> </div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> ;数据层 var data = { title: '<h2>点我!</h2>', color: 'red', img: { src: 'http://9.url.cn/edu/banner/img/880facff_760_300.jpg' } }; ;vm层 var vm = qvm.get({ selector: '#demo', data: { list: data }, action: function(){ alert('点击事件!'); } }); </script> ~~~ ## 10 q-*自定义directive ~~~ <style> .red{ color: red; } .green{ color: green; } </style> <div id="demo" q-repeat="list" q-on="click|action" q-self='selfProcess'> <div> <span q-class="color" q-text="title"></span> <span> <img q-attr-src="img.src" width="50" height="30"> </span> <div> </div> <script type="text/javascript" src="js/zepto.js"></script> <script type="text/javascript" src="js/qvm.js"></script> <script> ;数据层 var data = { title: '<h2>点我!</h2>', color: 'red', img: { src: 'http://9.url.cn/edu/banner/img/880facff_760_300.jpg' } }; ;vm层 var vm = qvm.get({ selector: '#demo', data: { list: data }, action: function(){ alert('点击事件!'); }, selfProcess: function(key, vm){ console.log(key, vm); alert('我是一个自定义处理指令!嘿嘿嘿~') } }); </script> ~~~ ## 11 参考 [qvm_api](https://ouvens.github.io/frontend-javascript/2015/08/20/mvvm-qvm-frame.html) [qvm完整代码](https://github.com/ouvens/qvm)