/source/app/assets/js/models/user.js

https://github.com/neekey/FreeNote · JavaScript · 261 lines · 150 code · 75 blank · 36 comment · 9 complexity · 990713509071bc220d895aa4dea6ab43 MD5 · raw file

  1. /**
  2. * model - user
  3. * @author Neekey<ni184775761@gmail.com>
  4. */
  5. //todo 将各model分离出来
  6. (function( APP ){
  7. var MODS = APP.mods,
  8. MODELS = APP.models;
  9. var Mtag = Backbone.Model.extend({
  10. defaults: {
  11. 'value': '',
  12. 'notes': [],
  13. 'x': 0,
  14. 'y': 0
  15. },
  16. initialize: function(){
  17. this.bind( 'change', function(){
  18. this.save();
  19. }, this );
  20. },
  21. localStorage: new MODS.localStorageStore( 'tag' ),
  22. /**
  23. * 添加笔记记录
  24. * @param id
  25. */
  26. addNote: function( id ){
  27. if( _.indexOf( this.get( 'notes' ), id ) < 0 ){
  28. var notes = _.clone( this.get( 'notes' ) );
  29. notes.push( id );
  30. this.set({ notes: notes });
  31. }
  32. },
  33. /**
  34. * 删除笔记记录
  35. * @param id
  36. */
  37. removeNote: function( id ){
  38. var index = _.indexOf( this.get( 'notes' ), id );
  39. if( index ){
  40. var notes = _.clone( this.get( 'notes' ) );
  41. notes.splice( index, 1 );
  42. this.set({ notes: notes });
  43. }
  44. }
  45. }),
  46. Mnote = Backbone.Model.extend({
  47. defaults: {
  48. 'content': '',
  49. 'tags': [],
  50. // 记录笔记在桌面上的显示位置
  51. 'x': 0,
  52. 'y': 0,
  53. 'created': '',
  54. 'updated': '',
  55. // 用于标识,该笔记是否在同步表里已经有相关记录
  56. 'syncMarked': false,
  57. // 用来标识,该笔记是否是从服务器中通不过来的
  58. 'fromServer': false
  59. },
  60. localStorage: new MODS.localStorageStore( 'note' ),
  61. initialize: function(){
  62. this.set({
  63. created: Date.now(),
  64. updated: Date.now()
  65. });
  66. this.bind( 'change', function(){
  67. this.set({ updated: Date.now() }, { silent: true });
  68. // 防止死循环...
  69. this.save({}, { silent: true } );
  70. }, this );
  71. this.save({}, { silent: true } );
  72. }
  73. }),
  74. CLtag = Backbone.Collection.extend({
  75. model: Mtag,
  76. defaults: {
  77. notes: null
  78. },
  79. initialize: function(){
  80. this.fetch();
  81. },
  82. init: function(){
  83. var Notes = this.notes;
  84. // 绑定笔记的tags信息的变化,自动更新到tags中去
  85. Notes.bind( 'change:tags', function( m, newTags ){
  86. var oldTags = m.previous( 'tags' ),
  87. addTags = _.difference( newTags, oldTags ), // 该笔记新添加的tag
  88. delTags = _.difference( oldTags, newTags );
  89. this.addNote( m.get( 'id' ), addTags );
  90. this.delNote( m.get( 'id' ), delTags );
  91. }, this );
  92. Notes.bind( 'add', function( m ){
  93. this.addNote( m.get( 'id' ), m.get( 'tags' ) );
  94. }, this );
  95. Notes.bind( 'remove', function( m ){
  96. this.delNote( m.get( 'id' ), m.get( 'tags' ) );
  97. }, this );
  98. },
  99. localStorage: new MODS.localStorageStore( 'tag' ),
  100. /**
  101. * 根据标签名称获取标签
  102. * @param name
  103. */
  104. getByName: function( name ){
  105. var tags = this.models,
  106. len = tags.length, i;
  107. for( i = 0; i < len; i++ ){
  108. if( tags[ i ].get( 'value' ) === name ){
  109. return tags[ i ];
  110. }
  111. }
  112. },
  113. /**
  114. * 向指定的标签数组中添加笔记记录
  115. * @param noteId
  116. * @param tags
  117. */
  118. addNote: function( noteId, tags ){
  119. var name, tag, i, len = tags.length;
  120. for( i = 0; i < len; i++ ){
  121. name = tags[ i ];
  122. tag = this.getByName( name );
  123. if( tag ){
  124. tag.addNote( noteId );
  125. }
  126. // 若标签不存在 则添加一个
  127. else {
  128. tag = this.create({
  129. value: name
  130. });
  131. tag.addNote( noteId );
  132. }
  133. }
  134. },
  135. /**
  136. * 从指定的标签中删除笔记记录
  137. * @param noteId
  138. * @param tags
  139. */
  140. delNote: function( noteId, tags ){
  141. var name, tag, i, len = tags.length;
  142. for( i = 0; i < len; i++ ){
  143. name = tags[ i ];
  144. tag = this.getByName( name );
  145. if( tag ){
  146. tag.removeNote( noteId );
  147. }
  148. }
  149. }
  150. }),
  151. CLnote = Backbone.Collection.extend({
  152. model: Mnote,
  153. localStorage: new MODS.localStorageStore( 'note' ),
  154. initialize: function(){
  155. // 创建笔记集合对应的标签集合
  156. var Tags = new CLtag();
  157. Tags.notes = this;
  158. Tags.init();
  159. // 读取已经有的笔记信息
  160. this.fetch();
  161. // 同步表
  162. var Sync = new MODELS[ 'sync' ]({
  163. notes: this
  164. });
  165. window[ 'Sync' ] = Sync;
  166. setInterval( function(){
  167. Sync.pushSync();
  168. }, 3000 );
  169. console.log( Sync.buildSyncTable() );
  170. }
  171. }),
  172. Muser = Backbone.Model.extend({
  173. defaults: {
  174. 'name': '',
  175. 'password': ''
  176. },
  177. urlRoot: '/res/user/',
  178. initialize: function(){
  179. }
  180. });
  181. MODELS[ 'tag' ] = Mtag;
  182. MODELS[ 'note' ] = Mnote;
  183. MODELS[ 'tags' ] = CLtag;
  184. MODELS[ 'notes' ] = CLnote;
  185. MODELS[ 'user' ] = Muser;
  186. })( window[ 'freenote' ] );