-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone.modelizer.coffee
More file actions
177 lines (126 loc) · 4.1 KB
/
Copy pathbackbone.modelizer.coffee
File metadata and controls
177 lines (126 loc) · 4.1 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
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
166
167
168
169
170
171
172
173
174
175
176
177
if require?
Backbone = require "backbone"
_ = require "underscore"
else
Backbone = window.Backbone
_ = window._
Backbone.IdentityMap =
# Global map
kinds: {}
maps: {}
# Get the identity map for a specific `kind` of model.
for: (kind) ->
id = null
_.any @kinds, (value, key) ->
if value == kind
id = key
return true
unless id?
@kinds[id = _.uniqueId("___identity_map")] = kind
@maps[id] ||= {}
# get an instance of `kind` with `id` if it exists.
retrieve: (kind, id) ->
map = @for kind
map[id]
# Store a new instance. Throw an error if it already exists.
store: (kind, id, obj) ->
map = @for kind
throw new Error "Model already cached!" if map[id]? and map[id] isnt obj
map[id] = obj
# Garbage-collect: delete all instances with refCount == 0
gc: ->
_.each @maps, (map) ->
_.each map, (obj, key) ->
delete map[key] if obj.refCount == 0
class Backbone.Model extends Backbone.Model
constructor: (attributes) ->
attributes = { id: attributes } if not _.isObject(attributes)
if attributes?.id?
cached = Backbone.IdentityMap.retrieve @constructor, attributes.id
if cached?
cached.set attributes if _.keys(attributes).length > 1
return cached
Backbone.IdentityMap.store @constructor, attributes.id, this
super attributes
# Attributes, meet associations. `modelize` is called during `set`
# and handles all the `associations` declared for this class.
# Subclasses can define associations:
#
# associations:
# account:
# model: App.Model.Account
#
# licenses:
# collection: App.Model.License
# url: "base/url" (optional)
# scope: "foo" (optional)
#
#
# scope parameter tells the model to add a reference
# to the calling model in the collection, e.g.
# collection.foo = this
modelize: (attributes = {}) =>
if _.isFunction @associations
associations = @associations()
else
associations = @associations
return if _.isEmpty associations
_.each associations, (association, name) =>
if association.model?
obj = attributes[name]
obj = obj.attributes if obj instanceof Backbone.Model
return unless obj?
@[name] = new association.model(obj)
attributes[name] = @[name].id
else
collection = attributes[name]
collection = collection.models if collection instanceof Backbone.Collection
if @[name]?
@[name].reset collection if _.isArray(collection)
else
self = this
class constructor extends association.collection
constructor: ->
if association.url?
@url = association.url
if association.scope?
@[association.scope] = self
super
@[name] = new constructor collection
if collection?
attributes[name] = _.compact _.map(collection, (el) ->
return el if not _.isObject(el)
el.id)
# Views use refcounting to expire things in the identity map.
refCount: 0
# Increase this model's refCount. Returns `this`.
retain: (obj) ->
++@refCount
if obj?
@references ||= []
@references.push obj
this
# Decrease this model's refCount.
release: (obj) ->
--@refCount
if obj?
@references = _.without @references, obj
this
# With ID map, a model can belong to several collections
# at once. Thus, we should never use the model's collection.url
# like backbone does..
url: ->
base = if _.isFunction(@urlRoot) then @urlRoot() else @urlRoot
throw new Error('A "urlRoot" property or function must be specified') unless base?
return base if @isNew()
sep = if base.charAt(base.length - 1) == "/" then "" else "/"
"#{base}#{sep}#{encodeURIComponent(@id)}"
set: (key, value, options) ->
if _.isObject(key) || key == null
attributes = key
options = value
else
attributes = {}
attributes[key] = value
@modelize attributes
super attributes, options