This repository has been archived by the owner on Jan 12, 2018. It is now read-only.
/
properties.coffee
165 lines (146 loc) · 5.59 KB
/
properties.coffee
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{Serenade} = require './serenade'
{Collection} = require './collection'
{AssociationCollection} = require './association_collection'
{Events} = require './events'
{pairToObject, serializeObject, extend, get} = require './helpers'
prefix = "_prop_"
exp = /^_prop_/
define = Object.defineProperties # we check for the plural because it is unsupported in buggy IE8
addGlobalDependencies = (object, dependency, names) ->
unless object["_glb_" + dependency]
object["_glb_" + dependency] = true
for name in names
if name.match(/\./)
type = "singular"
[name, subname] = name.split(".")
else if name.match(/:/)
type = "collection"
[name, subname] = name.split(":")
if subname
Serenade.globalDependencies[subname] or= []
Serenade.globalDependencies[subname].push({ object, dependency, subname, name, type })
addDependencies = (object, dependency, names) ->
names = [].concat(names)
for name in names
[name, subname] = name.split(/[:\.]/) if name.match(/[:\.]/)
object["_dep_" + name] ||= []
object["_dep_" + name].push(dependency) if object["_dep_" + name].indexOf(dependency) is -1
triggerGlobal = (object, names) ->
for name in names
if Serenade.globalDependencies[name]
for dependency in Serenade.globalDependencies[name]
if dependency.type is "singular"
if object is dependency.object.get(dependency.name)
triggerChangesTo(dependency.object, [dependency.dependency])
else if dependency.type is "collection"
if object in dependency.object.get(dependency.name)
triggerChangesTo(dependency.object, [dependency.dependency])
triggerChangesTo = (object, names) ->
findDependencies = (name) ->
dependencies = object["_dep_" + name]
if dependencies
for dependency in dependencies
if names.indexOf(dependency) is -1
names.push(dependency)
findDependencies(dependency)
findDependencies(name) for name in names
changes = {}
changes[name] = object.get(name) for name in names
object.trigger("change", changes)
triggerGlobal(object, names)
for own name, value of changes
object.trigger("change:#{name}", value)
Serenade.Properties =
property: (name, options={}) ->
@[prefix + name] = options
@[prefix + name].name = name
@[prefix + name].default = @[name] if @.hasOwnProperty(name)
addDependencies(this, name, options.dependsOn) if options.dependsOn
if define
Object.defineProperty @, name,
get: ->
addGlobalDependencies(this, name, [].concat(options.dependsOn)) if options.dependsOn
Serenade.Properties.get.call(this, name)
set: (value) -> Serenade.Properties.set.call(this, name, value)
if typeof(options.serialize) is 'string'
@property options.serialize,
get: -> @get(name)
set: (v) -> @set(name, v)
collection: (name, options={}) ->
extend options,
get: ->
unless @attributes[name]
@attributes[name] = new Collection([])
@attributes[name].bind 'change', =>
triggerChangesTo(this, [name])
@attributes[name]
set: (value) ->
@get(name).update(value)
@property name, options
set: (attributes, value) ->
attributes = pairToObject(attributes, value) if typeof(attributes) is 'string'
names = []
for name, value of attributes
names.push(name)
@attributes or= {}
if @[prefix + name]?.set
@[prefix + name].set.call(this, value)
else
@attributes[name] = value
triggerChangesTo(this, names)
get: (name, format) ->
@attributes or= {}
value = if @[prefix + name]?.get
@[prefix + name].get.call(this)
else if @[prefix + name]?.hasOwnProperty("default") and not @attributes.hasOwnProperty(name)
@[prefix + name].default
else
@attributes[name]
formatter = @[prefix + name]?.format
if format and typeof(formatter) is 'function'
formatter.call(this, value)
else
value
serialize: ->
serialized = {}
for name, options of this when name.match(exp)
if typeof(options.serialize) is 'string'
serialized[options.serialize] = serializeObject(@get(options.name))
else if typeof(options.serialize) is 'function'
[key, value] = options.serialize.call(@)
serialized[key] = serializeObject(value)
else if options.serialize
serialized[options.name] = serializeObject(@get(options.name))
serialized
extend(Serenade.Properties, Events)
Associations =
belongsTo: (name, attributes={}) ->
extend attributes,
set: (model) ->
if model.constructor is Object and attributes.as
model = new (attributes.as())(model)
@attributes[name] = model
@property name, attributes
@property name + 'Id',
get: -> get(@get(name), 'id')
set: (id) -> @attributes[name] = attributes.as().find(id)
dependsOn: name
serialize: attributes.serializeId
hasMany: (name, attributes={}) ->
extend attributes,
get: ->
unless @attributes[name]
@attributes[name] = new AssociationCollection(attributes.as, [])
@attributes[name].bind 'change', => triggerChangesTo(this, [name])
@attributes[name]
set: (value) ->
@get(name).update(value)
@property name, attributes
@property name + 'Ids',
get: -> new Collection(@get(name)).map((item) -> get(item, 'id'))
set: (ids) ->
objects = (attributes.as().find(id) for id in ids)
@get(name).update(objects)
dependsOn: name
serialize: attributes.serializeIds
exports.Associations = Associations