HTML5技术

初探React,将我们的View标签化 - 叶小钗(4)

字号+ 作者:H5之家 来源:H5之家 2015-10-08 08:23 我要评论( )

1 var app = app || {}; 2 3 ( function () { 4 'use strict' ; Utils = app.Utils; app.TodoModel = function (key) { 13 this .key = key; 14 this .todos = Utils.store(key); 15 this .onChanges = []; 16 }; 1

1 var app = app || {}; 2 3 (function () { 4 'use strict'; Utils = app.Utils; app.TodoModel = function (key) { 13 this.key = key; 14 this.todos = Utils.store(key); 15 this.onChanges = []; 16 }; 17 18 app.TodoModel.prototype.subscribe = function (onChange) { 19 this.onChanges.push(onChange); 20 }; 21 22 app.TodoModel.prototype.inform = function () { 23 Utils.store(this.key, this.todos); 24 this.onChanges.forEach(function (cb) { cb(); }); 25 }; 26 27 app.TodoModel.prototype.addTodo = function (title) { 28 this.todos = this.todos.concat({ 29 id: Utils.uuid(), 30 title: title, 31 completed: false 32 }); .inform(); 35 }; 36 37 app.TodoModel.prototype.toggleAll = function (checked) { .todos = this.todos.map(function (todo) { 43 return Utils.extend({}, todo, { completed: checked }); 44 }); .inform(); 47 }; 48 49 app.TodoModel.prototype.toggle = function (todoToToggle) { 50 this.todos = this.todos.map(function (todo) { 51 return todo !== todoToToggle ? 52 todo : 53 Utils.extend({}, todo, { completed: !todo.completed }); 54 }); .inform(); 57 }; 58 59 app.TodoModel.prototype.destroy = function (todo) { 60 this.todos = this.todos.filter(function (candidate) { 61 return candidate !== todo; 62 }); .inform(); 65 }; 66 67 app.TodoModel.prototype.save = function (todoToSave, text) { 68 this.todos = this.todos.map(function (todo) { 69 return todo !== todoToSave ? todo : Utils.extend({}, todo, { title: text }); 70 }); .inform(); 73 }; 74 75 app.TodoModel.prototype.clearCompleted = function () { 76 this.todos = this.todos.filter(function (todo) { 77 return !todo.completed; 78 }); .inform(); 81 }; 82 83 })();

utils为简单的工具类,不予理睬;无论什么时候数据层一定是MVC的重点,这里稍微给予一点关注:

① model层实现了一个简单的事件订阅通知系统

② 从类实现来说,他仅有三个属性,key(存储与localstorage的命名空间),todos(真实的数据对象),changes(事件集合)

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 谈一下我们是如何开展code review的 - HarlanC

    谈一下我们是如何开展code review的 - HarlanC

    2017-04-27 15:03

  • 【react学习】关于react框架使用的一些细节要点的思考 - 外婆的彭湖湾

    【react学习】关于react框架使用的一些细节要点的思考 - 外婆的彭湖

    2017-04-16 18:00

  • 【react框架】利用shouldComponentUpdate钩子函数优化react性能以及引入immutable库的

    【react框架】利用shouldComponentUpdate钩子函数优化react性能以及

    2017-04-16 09:02

  • 前端开发框架简介:angular和react - 腾讯云技术社区

    前端开发框架简介:angular和react - 腾讯云技术社区

    2017-04-11 18:02

网友点评
p