-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBaseModel.js
589 lines (515 loc) · 18.5 KB
/
BaseModel.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
* File: MC/data/BaseModel.js
*
* Override of Ext.data.Model to support inline associated data in Xtemplates and Forms
* Modified from https://github.com/robboerman/SenchaAssociationsPart1 to work with ExtJS 4
*
HISTORY
17-Sep-2013 JT McGibbon
Change linkAssociations function:
- associations.getName to associations.name
- associations.getType to associations.type
01-Nov-2013 JT McGibbon
Merged in MC.data.Model methods (ModusCreate GitHub: https://github.com/ModusCreateOrg/modus-create-sencha-plugin-pack/tree/master/MC/data
for writing heirarchy out on save:
* Works in conjunction with MC.data.JsonWriter to provide structured payloads during write operations. It will use
* the Model's mapping fields to build the payload rather than sending a flattened object. This will provide a solid
* base that should cover most use cases. Any Model can override this behavior as needed.
*
* Additionally this allows for hasMany and hasOne associations to be written in the same operation and using the same
* structured payload approach. Associations also can now have different names in read and write operations: specify
* both writeName and name if this behavior is needed.
*
* The default behavior of Ext JS is to use each field's persist property to determine whether it should be written.
* Any fields marked with persist false will be ignored. In create operations, all persistable fields are written and
* in update operations, only changes are written. We have added support for a forcePersist property to mark fields
* that should be written on update operations even if they have not changed.
01-Nov-2013 JT McGibbon
Added new setFlattenedData(data) method to allow updating of model using form data created using getFlattenedData approach (dot notation names)
or another baseModels getFlattenedData(true) output.
* Typical Usage: yourBaseModelInstance.setFlattenedData(form.getValues())
05-Nov-2013 JT McGibbon
Bug Fix: If hasMany associated store has records that are not dirty, do not increment the data array counter to
ensure that null array componets are not posted
08-Nov-2013 JT McGibbon
** NOTE: Removed due to rest error from CFML side -- need to fix first
Incorporated 4.2.2 Bug fix to use idProperty (the load() method in Ext4.2 wont work unless the id property is 'id'.)
Thanks to Forum user murrah (http://www.sencha.com/forum/member.php?13192-murrah)
and this forum item: Solved: Ext.data.Model - persisting data via proxy and without a store
Link:
http://www.sencha.com/forum/showthread.php?275404-Solved-Ext.data.Model-persisting-data-via-proxy-and-without-a-store&p=1008982#post1008982
*/
Ext.define("MC.data.BaseModel", {
extend: 'Ext.data.Model',
linkedAssociations: false,
writeStructuredData: true,
writeAllFields: true,
config: {
// Use uuid strategy for creating new ids
identifier: {
type: 'uuid'
}
},
/* 4.2.2 Bug fix
statics: {
load: function(id, config) {
//console.log('in bux.model load')
config = Ext.apply({}, config);
// Added this
var params={};
params[this.prototype.idProperty] = id;
config = Ext.applyIf(config, {
action: 'read',
params: params
});
// end Added
// Removed this
// config = Ext.applyIf(config, {
// action: 'read',
// id : id
//});
var operation = new Ext.data.Operation(config),
scope = config.scope || this,
callback;
callback = function(operation) {
var record = null,
success = operation.wasSuccessful();
if (success) {
record = operation.getRecords()[0];
// If the server didn't set the id, do it here
if (!record.hasId()) {
record.setId(id);
}
Ext.callback(config.success, scope, [record, operation]);
} else {
Ext.callback(config.failure, scope, [record, operation]);
}
Ext.callback(config.callback, scope, [record, operation, success]);
};
this.getProxy().read(operation, callback, this);
}
},
*/
inheritableStatics: {
/**
* The relation path mapper.
*
* @return A map of all the objects this model has relations to en their possible paths.
*/
// TODO: check if we really want to cache this.
getAllPaths: function() {
var name;
if(this.pathHistory) {
return this.pathHistory;
}
this.pathHistory = { };
name = this.modelName.substr(this.modelName.lastIndexOf('.') + 1);
this.pathHistory[name] = {
paths: [ [ name ] ]
};
this._getPaths(this, name, this.pathHistory);
return this.pathHistory;
},
_getPaths: function(root, mypath, history) {
var i, asoc, asocModel, name, sub;
// hasMany, belongsTo, hasOne
if(this.associations) {
// For every association.
for(i = 0; i < this.associations.all.length; i += 1) {
asoc = this.associations.all[i].config;
asocModel = Ext.ModelManager.getModel(asoc.associatedModel);
name = asoc.name;
sub = mypath + '.';
// We already know this object in our current path.
if(sub.search(new RegExp("(\\.|^)"+name+"\\.","g")) !== -1) {
continue;
}
sub += name;
// New path, record and descend.
if(!history[name] || !this._contains(history[name].paths, sub)) {
history[name] = history[name] || { paths: [] };
// Add a new path to object 'name' to list of paths to the object.
history[name].paths.push(sub.split('.'));
asocModel._getPaths(root, sub, history); // Descend.
}
}
}
},
getModelName: function() {
var name = this.getName();
return name.substr(name.lastIndexOf('.') + 1);
},
_contains: function(array, value) {
var i;
for(i = 0; i < array.length; i += 1) {
if(array[i] === value) {
return true;
}
}
return false;
}
},
getModelName: function() {
return this.self.getModelName();
},
/**
* Finds all associated records belonging to this instance.
*
* this uses the pathMapper and first path found from this object to it's destination.
* this uses a step by step finder using memory resident records.
* For every part in path
* get all records in current level from records from previous level
*
* Start level is the starting instance [ this ]
*
* @param modelName to find all records from.
* @return {Array} of associated records.
*/
getAssociatedRecords: function(modelName) {
var i, o, asoc, list, newList, split, paths = this.self.getAllPaths(), parent;
if(!paths[modelName]) {
throw new Error('There is no path between ' + this.getModelName() + ' and ' + modelName);
}
split = paths[modelName].paths[0];
list = [ this ]; // Start level
newList = []; // Next level.
// Skip the first entry(self);
for(i = 1; i < split.length; i += 1) { // Descend.
if(list.length === 0) {
return [];
}
// Find association for this level
asoc = list[0].associations.get(split[i]);
if(!asoc) {
throw new Error('Cannot find association ' + split[i] + ' on ' + list[0].getModelName());
}
// Get all sub records from current level.
for(o = 0; o < list.length; o += 1) {
if(asoc.getType().toLowerCase() === 'hasmany') {
newList = newList.concat(list[o].getChildren(split[i]));
} else {
parent = list[o].getParent(split[i]);
if(parent !== null) {
newList.push(parent);
}
}
}
list = newList; // Move next level to current level.
newList = [];
}
return list;
},
/* uses information from the associations to fetch a parent from an associated store */
getParent: function(assocName) {
var assoc = this.associations.get(assocName);
if (!assoc) {
return null;
}
var store = Ext.StoreMgr.get(assoc.config.foreignStore);
if (!store) {
return null;
}
return store.findRecordAll(assoc.config.primaryKey, this.get(assoc.config.foreignKey));
},
getChildren: function(assocName) {
var assoc = this.associations.get(assocName),
id = this.get(assoc.config.primaryKey);
if (!assoc) {
return null;
}
var store = Ext.StoreMgr.get(assoc.config.foreignStore);
if (!store) {
return null;
}
return store.findRecordsAll(function(record) {
return record.get(assoc.config.foreignKey) === id;
});
},
getChildrenData: function(assocName){
var records = this.getChildren(assocName);
var rt = [];
for(var i in records){
rt.push(records[i].data);
}
return rt;
},
/* warning, recursive down in combination with up can be dangerous when there are loops in associations */
getData: function(includeAssociated,down) {
if (includeAssociated && !this.linkedAssociations) {
this.linkedAssociations = true;
this.linkAssociations(includeAssociated);
}
var data = this.callParent(arguments);
if (down) {
var childData = this.getAllChildData();
Ext.apply(data, childData);
}
return data;
},
getRawData: function(strict) {
var i, meta, data = Ext.apply({}, this.getData(false));
if(!SalesForce.metaStore[this.self.getSfName()]) {
return data;
}
meta = SalesForce.metaStore[this.self.getSfName()];
for(i in data) {
if(!meta.fieldMap[i] || (strict && !meta.fieldMap[i].createable)) {
delete data[i];
}
}
return data;
},
getFlattenedData: function(includeAssociated) {
var data = this.getData(includeAssociated, false); // don't ever recurse down when getting flattened data!
/* This function flattens the datastructure of am object such that it can be used in a form
* {foo:1,bar:{blah: {boo: 3}}} becomes {foo: 1, bar.blah.boo: 3}
* This is the only way to use associated data in a form
* thanks to http://stackoverflow.com/users/2214/matthew-crumley
*/
var count=1;
var prop;
var flatten = function(obj, includePrototype, into, prefix) {
if (count++ > 20) {console.log('TOO DEEP RECURSION'); return;} // prevent infinite recursion
into = into || {};
prefix = prefix || "";
for (var k in obj) {
if (includePrototype || obj.hasOwnProperty(k)) {
var prop = obj[k];
if (prop instanceof Array) { continue; } // Don't recurse into hasMany relations
if (prop && typeof prop === "object" &&
!(prop instanceof Date || prop instanceof RegExp)) {
flatten(prop, includePrototype, into, prefix + k + ".");
}
else {
into[prefix + k] = prop;
}
}
}
return into;
};
return flatten(data, false);
},
/* this function ONLY recurses upwards (belongsTo), otherwise the data structure could become infinite */
linkAssociations: function(includeAssociated, count) {
var me = this,
associations = this.associations.items,
associationCount = associations.length,
associationName,
association,
associatedRecord,
i,
type,
foreignStore;
count = count || 0;
//debugger;
if (count > 10) {
console.log('Too deep recursion in linkAssociations');
return;
}
for (i = 0; i < associationCount; i++) {
association = associations[i];
associationName = association.name; /* jtm: 09/17/2013 */
type = association.type; /* jtm: 09/17/2013 */
foreignStore = association.config.foreignStore;
if (!foreignStore) {
continue;
}
if (type.toLowerCase() === 'belongsto' || type.toLowerCase() === 'hasone') {
associatedRecord = this.getParent(associationName);
if (associatedRecord) {
this[association.getInstanceName()] = associatedRecord;
associatedRecord.linkAssociations(includeAssociated, (count+1));
} else if (this.get(association.config.foreignKey)) {
console.log('Warning, model association not found ');
}
}
}
},
getAllChildData: function() {
var associations = this.associations.items,
associationCount = associations.length,
associationName,
association,
i,
type,
foreignStore,
childData = {};
for (i = 0; i < associationCount; i++) {
association = associations[i];
associationName = association.getName();
type = association.getType();
foreignStore = association.config.foreignStore;
if (!foreignStore) {
continue;
}
if (type.toLowerCase() == 'hasmany') {
var children = this.getChildrenData(associationName);
childData[associationName] = children;
}
}
return childData;
},
/**
* setFlattenedData(data) method to allow updating of baseModel using form data created using getFlattenedData approach (dot notation names)
* or another baseModels getFlattenedData(true) output.
* * Typical Usage: yourBaseModelInstance.setFlattenedData( form.getValues() )
*
* @param {object} values - model field members and values to set
* @return {object} this.getFlattenedData(true) - (updated data set) or empty object if values param not an object
*/
setFlattenedData: function(values) {
var sFuncs=[],aName=[],dFuncs=[],
i=0,j=0,
name,sFnc,sDirty;
if(typeof values != 'object'){
return {};
} else {
this.setDirty();
for (name in values) {
if(typeof values[name] != 'object'){
sFnc='this.';
sDirty='this.';
aName = name.split('.')
while(aName.length > 1){
sFnc=sFnc+'get'+aName[0]+'().';
sDirty=sDirty+'get'+aName[0]+'().';
dFuncs[j] = sDirty+'setDirty()';
j++;
aName.shift();
}
sFnc=sFnc+'set("'+aName[0]+'","'+values[name]+'")';
sFuncs[i]=sFnc;
i++;
}
}
for(i=0;i<sFuncs.length;i++){
eval(sFuncs[i]);
}
for(i=0;i<dFuncs.length;i++){
eval(dFuncs[i]);
}
return this.getFlattenedData(true);
}
},
// MC.data.Model methods
getWriteData: function() {
var data = this.getRecordWriteData(this),
associations = this.associations.items,
association, type, name, associatedStore, associatedRecords, associatedRecord,
i, len, j, jLen, jDi
phantom = this.phantom,
writeAllFields = this.writeAllFields;
for (i=0, len=associations.length; i<len; i++) {
association = associations[i];
type = association.type;
name = association.name;
// Use associationKey as a cue that the data is in-line (structured)
if ( association.associationKey ) {
if (type == 'hasMany') {
associatedStore = this[association.storeName];
if (associatedStore) {
//Only write the association if it's an insert, it's specifically required or there are changes
if (phantom || writeAllFields || associatedStore.getModifiedRecords().length > 0) {
//if it's loaded, put it into the association data
if (associatedStore.getCount() > 0) {
//we will use this to contain each associated record's data
data[name] = [];
associatedRecords = associatedStore.data.items;
//now we're finally iterating over the records in the association. Get
// all the records so we can process them
for (j=0, jDi = 0,jLen=associatedRecords.length; j<jLen; j++) {
// Only write back dirty items
if (associatedRecords[j].dirty){
data[name][jDi] = this.getRecordWriteData(associatedRecords[j]);
jDi++; // jtm - 11-05-2013
}
}
// Get rid of if no records
if(data[name].length == 0){
delete data[name];
}
}
}
}
} else if (type == 'hasOne') {
associatedRecord = this[association.instanceName];
// If we have a record and it has changed, put it onto our list
if (associatedRecord !== undefined && associatedRecord.dirty ) {
//data[name] = this.getRecordWriteData(associatedRecord);
data[name] = associatedRecord.getWriteData();
}
}
}
}
return data;
},
getRecordWriteData: function(record) {
var isPhantom = record.phantom === true,
writeAllFields = record.proxy.writer.writeAllFields || isPhantom,
fields = record.fields,
fieldItems = fields.items,
data = {},
changes,
field,
key,
f, fLen,
forcePersist;
changes = record.getChanges();
for (f=0, fLen=fieldItems.length; f<fLen; f++) {
field = fieldItems[f];
if (field.forcePersist || (field.persist && writeAllFields)) {
this.setFieldWriteData(data, record, field);
}
}
for (key in changes) {
if (changes.hasOwnProperty(key)) {
field = fields.get(key);
if (field.persist) {
this.setFieldWriteData(data, record, field, changes[key]);
}
}
}
return data;
},
setFieldWriteData: function(data, record, field, value) {
var name = field[this.nameProperty] || field.name,
path, i, len, curr;
if (!value) {
value = record.get(field.name);
}
// Skip the id field for phantom records
if (field.name === record.idProperty && record.phantom) {
return;
}
if (field.mapping) {
if (field.mapping.indexOf('.')) {
path = field.mapping.split('.');
curr = data;
for (i=0, len=path.length-1; i<len; i++) {
if (!curr[path[i]]) {
curr[path[i]] = {};
}
curr = curr[path[i]];
}
curr[path[i]] = value;
}
} else {
data[name] = value;
}
},
/**
* Convenience method to set values to the model instance and save them via the proxy
*
* @param {Object} values An object of the properties to set. Same as for Model.set()
* @param {Object} options The same options you can pass to Model.save()
* @return {Ext.bux.data.Model} The model instance
*/
saveValuesViaProxy : function(values,options){
// The save() triggers the PUT, no autoSync available on the model it appears
// See http://edspencer.net/2011/02/02/proxies-extjs-4/
// options passed in can include a callback. Is Ext.data.Operation config
// see http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Model-method-save
this.set(values);
return this.save(options);
}
});