Repository navigation
Expand file tree
/
Copy pathmodel.coffee
More file actions
executable file
·127 lines (101 loc) · 3.02 KB
/
Copy pathmodel.coffee
File metadata and controls
executable file
·127 lines (101 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict'
{EventEmitter} = require 'microemitter'
module.exports = class Model extends EventEmitter
# contrib
MongoOp = require 'mongoop'
JsPath = require 'jspath'
createId = @createId = require 'hat'
Traverse = require 'traverse'
{extend} = require './util'
@isOpaque =-> no
@streamModels =(selector, options, callback)->
unless 'each' of this then throw new Error """
streamModels depends on Model#each, but cursor was not found!
(Hint: it may not be whitelisted)
"""
ids = []
@each selector, options, (err, model)->
if err then callback err
else if model?
ids.push model.getId?()
callback err, [model]
else
callback null, null, ids
mixin: @mixin = (source)->
@[key] = val for key,val of source when key isnt 'constructor'
watch:(field, watcher)->
@watchers[field] or= []
@watchers[field].push watcher
unwatch:(field, watcher)->
unless watcher
delete @watchers[field]
else
index = @watchers.indexOf watcher
@watchers.splice index, 1 if ~index
init:(data)->
model = @
model.watchers = {}
model.bongo_ or= {}
if data?
model.set data
unless 'instanceId' of model.bongo_
model.bongo_.instanceId = createId()
@emit 'init'
@on 'updateInstance', (data) =>
if Encoder?.XSSEncode?
data = new Traverse(data).map (node) ->
return Encoder.XSSEncode node if 'string' is typeof node
return node
@update_ data
set:(data={})->
model = @
delete data.data
# model.data or= {}
extend model, data
model
getFlagValue:(flagName)->
@flags_?[flagName]
watchFlagValue:(flagName, callback)->
@watch "flags_.#{flagName}", callback
unwatchFlagValue:(flagName)->
@unwatch "flags_.#{flagName}"
decoded:
if `Encoder`?
then (path)-> `Encoder`.htmlDecode @getAt path
else @::getAt
getAt:(path)-> JsPath.getAt @, path
setAt:(path, value)->
JsPath.setAt @, path, value
@emit 'update', [path]
getId:-> @_id
getToken: -> @token or @getId()
getSubscribable:->
{subscribable} = @bongo_
return subscribable if subscribable?
return true
equals:(model)->
if @getId and model?.getId
@getId() is model.getId()
else
@ is model
valueOf: -> @getValue?() ? this # magical valueOf kludge
save:(callback)->
model = @
model.save_ (err, docs)->
if err
callback err
else
extend model, docs[0] # replace local values with server-validated ones.
bongo.addReferences model
callback null, docs
# emit:(event, rest...)->
# listeners = @multiplexer.events[event]?.listeners || []
# listener.apply @, rest for listener in listeners
update_:(data)->
fields = new MongoOp(data).applyTo @
Object.keys(fields).forEach (field)=>
@watchers[field]?.forEach (watcher)=> watcher.call @, fields[field]
@emit 'update', (Object.keys fields.result)
# alias these:
addListener : @::on
removeListener : @::off