Skip to content

Instantly share code, notes, and snippets.

@jratcliff
Last active April 18, 2018 21:05
Show Gist options
  • Save jratcliff/0200d0ead2adbf607d256bc09696e2e0 to your computer and use it in GitHub Desktop.
Save jratcliff/0200d0ead2adbf607d256bc09696e2e0 to your computer and use it in GitHub Desktop.
/**
* Overrides for Ext.data.schema.Role
*/
Ext.define('overrides.data.schema.Role', {
override: 'Ext.data.schema.Role',
/**
* The default field name that gets updated with the current time when
* an associated record changes. This can be changed by configuring this
* on the record that has the 'useAssociationEditPropagation' config set to true.
*
* @example
* Ext.define('MyApp.model.MyCustomModel', {
* extend: 'Ext.data.Model',
* useAssociationEditPropagation: true,
* associationChangeField: 'lastAssociationUpdated' // set this if you want to use a different field
* });
*/
associationChangeField: '_associationChange',
/**
* Override that will add store listeners for HasMany associations if these
* associations or the model itself has a useAssociationEditPropagation config
* set to true. These store listeners will then update a non-persisted field
* in the parent records so that changes to records in HasMany stores will
* trigger the same events as changes in the parent records themselves and will
* set the 'dirty' flag correctly on records.
*/
createAssociationStore: function (session, from, records, isComplete) {
var me = this,
association = me.association,
useAssociationEditPropagation = association.useAssociationEditPropagation || from.useAssociationEditPropagation,
store = me.callParent(arguments);
// add store listeners to notify when any association model changes.
if (useAssociationEditPropagation) {
me.addStoreChangedListeners(store);
}
return store;
},
addStoreChangedListeners: function (associationStore) {
var me = this;
associationStore.on({
add: 'onHasManyAssociationChange',
update: 'onHasManyAssociationChange',
remove: 'onHasManyAssociationRemove',
clear: 'onHasManyAssociationRemove',
scope: me
});
},
onHasManyAssociationChange: function (store, records) {
var parentRec = store.getAssociatedEntity(),
associationChangeField = parentRec.associationChangeField || this.associationChangeField,
inverseSetterName = this.inverse.setterName,
rLen, record, r,
fieldsMap, field, savedPersist;
if (parentRec) {
fieldsMap = parentRec.fieldsMap;
field = fieldsMap[associationChangeField];
savedPersist = field.persist;
field.persist = true; // this is so that this field gets added to this.modified
parentRec.set(associationChangeField, new Date());
field.persist = savedPersist;
// make sure each record still has a link back to the parentRec
if (inverseSetterName) {
records = Ext.isArray(records) ? records : [records];
rLen = records.length;
for (r = 0; r < rLen; r++) {
record = records[r];
record[inverseSetterName](parentRec);
}
}
}
},
onHasManyAssociationRemove: function (store, records) {
var parentRec = store.getAssociatedEntity(),
associationChangeField = parentRec.associationChangeField || this.associationChangeField,
fieldsMap, field, savedPersist;
if (parentRec) {
fieldsMap = parentRec.fieldsMap;
field = fieldsMap[associationChangeField];
savedPersist = field.persist;
field.persist = true; // this is so that this field gets added to this.modified
parentRec.set(associationChangeField, new Date());
field.persist = savedPersist;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment