Created
September 25, 2012 10:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'd use it something like...