forked from erichocean/blossom
/
object.js
342 lines (272 loc) · 11.7 KB
/
object.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2010 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
sc_require('controllers/controller') ;
/** @class
An ObjectController gives you a simple way to manage the editing state of
an object. You can use an ObjectController instance as a "proxy" for your
model objects.
Any properties you get or set on the object controller, will be passed
through to its content object. This allows you to setup bindings to your
object controller one time for all of your views and then swap out the
content as needed.
h2. Working with Arrays
An ObjectController can accept both arrays and single objects as content.
If the content is an array, the ObjectController will do its best to treat
the array as a single object. For example, if you set the content of an
ObjectController to an array of Contact records and then call:
contactController.get('name');
The controller will check the name property of each Contact in the array.
If the value of the property for each Contact is the same, that value will
be returned. If the any values are different, then an array will be
returned with the values from each Contact in them.
Most SproutCore views can work with both arrays and single content, which
means that most of the time, you can simply hook up your views and this will
work.
If you would prefer to make sure that your ObjectController is always
working with a single object and you are using bindings, you can always
setup your bindings so that they will convert the content to a single object
like so:
contentBinding: SC.Binding.single('MyApp.listController.selection') ;
This will ensure that your content property is always a single object
instead of an array.
@extends SC.Controller
@since SproutCore 1.0
*/
SC.ObjectController = SC.Controller.extend(
/** @scope SC.ObjectController.prototype */ {
// ..........................................................
// PROPERTIES
//
/**
Set to the object you want this controller to manage. The object should
usually be a single value; not an array or enumerable. If you do supply
an array or enumerable with a single item in it, the ObjectController
will manage that single item.
Usually your content object should implement the SC.Observable mixin, but
this is not required. All SC.Object-based objects support SC.Observable
@property {Object}
*/
content: null,
/**
If YES, then setting the content to an enumerable or an array with more
than one item will cause the Controller to attempt to treat the array as
a single object. Use of get(), for example, will get every property on
the enumerable and return it. set() will set the property on every item
in the enumerable.
If NO, then setting content to an enumerable with multiple items will be
treated like setting a null value. hasContent will be NO.
@property {Boolean}
*/
allowsMultipleContent: NO,
/**
Becomes YES whenever this object is managing content. Usually this means
the content property contains a single object or an array or enumerable
with a single item. Array's or enumerables with multiple items will
normally make this property NO unless allowsMultipleContent is YES.
@property {Boolean}
*/
hasContent: function() {
return !SC.none(this.get('observableContent'));
}.property('observableContent'),
/**
Makes a controller editable or not editable. The SC.Controller class
itself does not do anything with this property but subclasses will
respect it when modifying content.
@property {Boolean}
*/
isEditable: YES,
/**
Primarily for internal use. Normally you should not access this property
directly.
Returns the actual observable object proxied by this controller. Usually
this property will mirror the content property. In some cases - notably
when setting content to an enumerable, this may return a different object.
Note that if you set the content to an enumerable which itself contains
enumerables and allowsMultipleContent is NO, this will become null.
@property {Object}
*/
observableContent: function() {
var content = this.get('content'),
len, allowsMultiple;
// if enumerable, extract the first item or possibly become null
if (content && content.isEnumerable) {
len = content.get('length');
allowsMultiple = this.get('allowsMultipleContent');
if (len === 1) content = content.firstObject();
else if (len===0 || !allowsMultiple) content = null;
// if we got some new content, it better not be enum also...
if (content && !allowsMultiple && content.isEnumerable) content=null;
}
return content;
}.property('content', 'allowsMultipleContent').cacheable(),
// ..........................................................
// METHODS
//
/**
Override this method to destroy the selected object.
The default just passes this call onto the content object if it supports
it, and then sets the content to null.
Unlike most calls to destroy() this will not actually destroy the
controller itself; only the the content. You continue to use the
controller by setting the content to a new value.
@returns {SC.ObjectController} receiver
*/
destroy: function() {
var content = this.get('observableContent') ;
if (content && SC.typeOf(content.destroy) === SC.T_FUNCTION) {
content.destroy();
}
this.set('content', null) ;
return this;
},
/**
Invoked whenever any property on the content object changes.
The default implementation will simply notify any observers that the
property has changed. You can override this method if you need to do
some custom work when the content property changes.
If you have set the content property to an enumerable with multiple
objects and you set allowsMultipleContent to YES, this method will be
called anytime any property in the set changes.
If all properties have changed on the content or if the content itself
has changed, this method will be called with a key of "*".
@param {Object} target the content object
@param {String} key the property that changes
@returns {void}
*/
contentPropertyDidChange: function(target, key) {
if (key === '*') this.allPropertiesDidChange();
else this.notifyPropertyChange(key);
},
/**
Called whenver you try to get/set an unknown property. The default
implementation will pass through to the underlying content object but
you can override this method to do some other kind of processing if
needed.
@property {String} key key being retrieved
@property {Object} value value to set or undefined if reading only
@returns {Object} property value
*/
unknownProperty: function(key,value) {
// avoid circular references
if (key==='content') {
if (value !== undefined) this.content = value;
return this.content;
}
// for all other keys, just pass through to the observable object if
// there is one. Use getEach() and setEach() on enumerable objects.
var content = this.get('observableContent'), loc, cur, isSame;
if (content===null || content===undefined) return undefined; // empty
// getter...
if (value === undefined) {
if (content.isEnumerable) {
value = content.getEach(key);
// iterate over array to see if all values are the same. if so, then
// just return that value
loc = value.get('length');
if (loc>0) {
isSame = YES;
cur = value.objectAt(0);
while((--loc > 0) && isSame) {
if (cur !== value.objectAt(loc)) isSame = NO ;
}
if (isSame) value = cur;
} else value = undefined; // empty array.
} else value = (content.isObservable) ? content.get(key) : content[key];
// setter
} else {
if (!this.get('isEditable')) {
throw "%@.%@ is not editable".fmt(this,key);
}
if (content.isEnumerable) content.setEach(key, value);
else if (content.isObservable) content.set(key, value);
else content[key] = value;
}
return value;
},
// ...............................
// INTERNAL SUPPORT
//
/** @private - setup observer on init if needed. */
init: function() {
sc_super();
if (this.get('content')) this._scoc_contentDidChange();
if (this.get('observableContent')) this._scoc_observableContentDidChange();
},
_scoc_contentDidChange: function () {
var last = this._scoc_content,
cur = this.get('content');
if (last !== cur) {
this._scoc_content = cur;
var func = this._scoc_enumerableContentDidChange;
if (last && last.isEnumerable) {
//console.log('no longer observing [] on last');
last.removeObserver('[]', this, func);
}
if (cur && cur.isEnumerable) {
//console.log('observing [] on cur');
cur.addObserver('[]', this, func);
}
}
}.observes("content"),
/** @private
Called whenever the observable content property changes. This will setup
observers on the content if needed.
*/
_scoc_observableContentDidChange: function() {
var last = this._scoc_observableContent,
cur = this.get('observableContent'),
func = this.contentPropertyDidChange,
efunc= this._scoc_enumerableContentDidChange;
if (last === cur) return this; // nothing to do
//console.log('observableContentDidChange');
this._scoc_observableContent = cur; // save old content
// stop observing last item -- if enumerable stop observing set
if (last) {
if (last.isEnumerable) last.removeObserver('[]', this, efunc);
else if (last.isObservable) last.removeObserver('*', this, func);
}
if (cur) {
if (cur.isEnumerable) cur.addObserver('[]', this, efunc);
else if (cur.isObservable) cur.addObserver('*', this, func);
}
// notify!
if ((last && last.isEnumerable) || (cur && cur.isEnumerable)) {
this._scoc_enumerableContentDidChange();
} else this.contentPropertyDidChange(cur, '*');
}.observes("observableContent"),
/** @private
Called when observed enumerable content has changed. This will teardown
and setup observers on the enumerable content items and then calls
contentPropertyDidChange(). This method may be called even if the new
'cur' is not enumerable but the last content was enumerable.
*/
_scoc_enumerableContentDidChange: function() {
var cur = this.get('observableContent'),
set = this._scoc_observableContentItems,
func = this.contentPropertyDidChange;
// stop observing each old item
if (set) {
set.forEach(function(item) {
if (item.isObservable) item.removeObserver('*', this, func);
}, this);
set.clear();
}
// start observing new items if needed
if (cur && cur.isEnumerable) {
if (!set) set = SC.Set.create();
cur.forEach(function(item) {
if (set.contains(item)) return ; // nothing to do
set.add(item);
if (item.isObservable) item.addObserver('*', this, func);
}, this);
} else set = null;
this._scoc_observableContentItems = set; // save for later cleanup
// notify
this.contentPropertyDidChange(cur, '*');
return this ;
}
}) ;