7. 服务 -- Highway MVVM
来源:互联网 发布:csgom4a1皮肤知乎 编辑:程序博客网 时间:2024/06/16 13:56
7-1. 内建
7-1-1. $scope
参考2-4. 作用域章节
7-1-2. $timeout
<!-- examples/services/timeout.html --><div>something happend after {{timeout}}s</div>const app = new Highway({ $el: $('#app'), $scope: { timeout: 3 }, $mount() { this.$timeout(() => { alert('highway'); }, 3000); }});
7-1-2. $interval
<!-- examples/services/timeout.html --><div>something happend after {{timeout}}s</div>const app = new Highway({ $el: $('#app'), $scope: { timeout: 3 }, $mount() { this.$timeout(() => { alert('highway'); }, 3000); }});
7-1-3. $http
复用jQuery/Zepto中的ajax方法
http: .ajaxget: .getpost: .postjson: .getJSONjsonp: .ajaxJSONPsettings: .ajaxSettingssettings:jQuery.ajaxSetup/ .ajaxSettings
<!-- examples/services/http.html --><ul> <li hi-repeat="user in users">id: {{user.id}}, name: {{user.name}}</li></ul>const app = new Highway({ $el: $('#app'), $scope: {}, $mount() { this.$http({ type: 'get', url: './users.json', success: (data, status, xhr) => { this.$set('users', data.users) }, error: (xhr, errorType, error) => { alert('error'); } }) }});
7.1.4. $event
参阅6-3. 通信
7-2. 自定义
自定义服务是一个工厂函数
7-2-1. 入参
- $ctx: 当前上下文
7-2-2. 出参
可选。如返回,可通过this.$servcies[name]引用
$mount: 生命周期函数,挂载时调用
$unmount: 生命周期函数,销毁时调用
7-2-3. 全局
使用Highway.service定义,需指定宏服务名和服务工厂函数
7-2-4. 局部
指定视图中有效,通过$services指定,需指定宏指令名和宏指令工厂函数
7-2-5. 示例
定义一个cookie读写服务
<!-- examples/services/customized.html --><div id="app"> <button hi-on:click="clickMe">destroy me</button></div>const cookie = function ({$ctx}) { return $ctx.$cookie = { $mount() { console.log('>>> cookie services mounted') }, $get(key) { const reg = new RegExp(`(^| )${key}=([^;]*)(;|$)`); const matches = document.cookie.match(reg); if(matches) { return matches[2] } }, $set(key, value) { document.cookie = `${key}=${value}`; }, $unmount() { console.log('>>> cookie services unmounted') }, destroy() { this.$destory(); } }};// 全局管道//Highway.service('$cookie', cookie);const app = new Highway({ $el: $('#app'), $scope: { ratio: 0.95 }, // 局部管道 $services: { $cookie: cookie }, $mount() { this.$cookie.$set('cookie_0', 'highway'); this.$cookie.$set('cookie_1', 'hello world'); // 可通过$service引用 console.log('cookie_1 value is:' + this.$services['$cookie'].$get('cookie_1')); }, clickMe() { this.$destroy(); }});
0 0
- 7. 服务 -- Highway MVVM
- 0. 前言 -- Highway MVVM
- 2. 视图 -- Highway MVVM
- 3. 指令 -- Highway MVVM
- 5. 管道 -- Highway MVVM
- 6. 组件 -- Highway MVVM
- 8. 工具 -- Highway MVVM
- 1. 快速开始 -- Highway MVVM
- 4. 宏指令 -- Highway MVVM
- Highway
- pku1648Countryside Highway
- poj2485 Highway
- hihoCoder1090 Highway
- Highway Networks
- Highway Network
- poj1751 highway
- Highway Networks
- Highway Networks
- 模拟泛型DAO增删改查--->jdbc实现
- 无误的Mat2QImage代码
- 5. 管道 -- Highway MVVM
- 6. 组件 -- Highway MVVM
- 浅谈正则化
- 7. 服务 -- Highway MVVM
- Spring XML小结
- tensorflow 使用nearest最邻近算法 分类mnist数据库
- 一款社交教育类问答游戏的在线匹配问题
- 8. 工具 -- Highway MVVM
- 扩展已知类的功能
- Canvas
- 进程操作
- CentOS修改mysql 用户root的密码并允许远程登录