Skip to content

Instantly share code, notes, and snippets.

@subimage
Created September 25, 2012 10:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subimage/3781077 to your computer and use it in GitHub Desktop.
Save subimage/3781077 to your computer and use it in GitHub Desktop.
Sencha Touch 2 "child store" that keeps data sync'd from a parent Ext.data.Store
// A sub-store implementation that allows you to store local
// cached records in a 'parentStore', and then use this to present
// a view you can filter on for DataViews like List, etc.
//
// Dynamically selects data from parentStore, and refreshes itself
// when data in that store is changed.
//
// Allows passing a queryFunc to restrict selection of records
// from the parentStore.
Ext.define('CbMobile.store.ChildView', {
extend: 'Ext.data.Store',
parentStore: null,
queryFunc: null,
constructor: function(config) {
config || (config == {});
this.parentStore = config['parentStore'];
this.queryFunc = config['queryFunc'];
if(!this.parentStore) {
Ext.Logger.warn('parentStore should be set in config for store.ChildView');
return;
}
this.callParent();
// Bind & load data
this.parentStore.on('addrecords', this.loadDataFromParent, this);
this.parentStore.on('removerecords', this.loadDataFromParent, this);
this.parentStore.on('updaterecord', this.loadDataFromParent, this);
this.parentStore.on('refresh', this.loadDataFromParent, this);
this.loadDataFromParent();
},
// Loads data from parent store.
loadDataFromParent: function() {
var me=this;
this.removeAll();
if(this.queryFunc) {
var results = this.parentStore.queryBy(function(record, id){
return me.queryFunc(record, id)
});
this.add(results.getRange());
} else {
this.add(this.parentStore.getRange());
}
}
});
@subimage
Copy link
Author

You'd use it something like...

var subStore = new CbMobile.store.ChildView({
  parentStore: Ext.getStore('SomeExistingStore')
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment