-
9 Jan 2012 3:57 PM #1
Answered: Filtering a store upon view instantiation
Answered: Filtering a store upon view instantiation
I feel like I am starting to get the hang of the MVC methodology in ST2, and enjoy my mini-victories when something actually shows up on the screen. That being said, I've run into a problem that has me stumped, despite extensive searching.
I have a view that contains two lists (specifically, one list with clocked in users, one with clocked out users). I have a User model and a Users store backing the lists and providing data. Currently, if I assign both lists to use store: 'Users' it works just fine. Here is the code I'm using in the view to instantiate one of the lists:
However, I want to tell each list to filter the store down to just those who are clocked in or out by checking the boolean value in the model. I know I can assign a store via either store: 'Users' or store: Ext.create('MyApp.store.Users'), but then when I try to chain a filter method on to the create() for example, I get a 'Cannot call method 'getGroups' of null, so I'm guessing the store object isn't created just yet.PHP Code:{
xtype: 'list',
flex: 1,
cls: 'clockedInList',
itemTpl: '<span class="employee">{lastName}, {firstName}</span>',
grouped: true,
indexBar: false,
store: 'Users'
}
I've even tried defining store: 'Users', and then hooking into the initialization() method to set a new one, but the list disappears when I try this (see code below).
Could someone shed some light on the proper way to do this, and maybe fill in the holes in my understanding? Thanks so much!PHP Code:store: 'Users',
initialize: function() {
console.log('running initializes...');
var store = Ext.getStore("Users");
if (!store) {
console.log('store not found, creating');
store = Ext.create("Users");
} else console.log('store found, using existing...');
this.store = store;
this.callParent(arguments);
}
-
Best Answer Posted by jlyman
(darn, had a good long post written up and the page ate it on preview, so here's a condensed version):
Looks like this isn't possible how I was envisioning it at the time. Instead, it sounds like you need to create a base store, and then use that base store to load sub-stores manually (original idea found here: http://www.sencha.com/forum/showthre...ferent-filters).
So for example, here's the code from my base model (Users.js) which manually loads data into my two sub-stores (UsersIn.js, UsersOut.js). See the constructor object for the actually loading:
PHP Code:Ext.define('app.store.Users', {
extend: 'Ext.data.Store',
model: 'app.model.User',
sorters: 'lastName',
autoLoad: true,
getGroupString: function(record) {
return record.get('lastName')[0];
},
constructor: function () {
this.callParent(arguments);
// Load the sub stores from this one based on the data loaded.
this.on('load', function (store, records, successful, operation, opts) {
var inStore = Ext.getStore('UsersIn');
var outStore = Ext.getStore('UsersOut');
inStore.removeAll();
outStore.removeAll();
for (var i=0; i<records.length; i++) {
if (records[i].data.clockedIn != '')
inStore.add(records[i]);
else
outStore.add(records[i]);
}
}, this);
}
});
-
10 Jan 2012 5:32 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
Are you wanting to use the same store instance or each list have it's own store?
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
10 Jan 2012 6:56 AM #3
Preferably, I'd like both lists to use the same store, just filtered. That way I only have to make one request to fill it instead of two, and management would be a lot easier.
-
11 Jan 2012 2:41 PM #4
Any pointers anyone? I'd appreciate it!
-
16 Jan 2012 3:11 PM #5
How it can potentially be done
How it can potentially be done
(darn, had a good long post written up and the page ate it on preview, so here's a condensed version):
Looks like this isn't possible how I was envisioning it at the time. Instead, it sounds like you need to create a base store, and then use that base store to load sub-stores manually (original idea found here: http://www.sencha.com/forum/showthre...ferent-filters).
So for example, here's the code from my base model (Users.js) which manually loads data into my two sub-stores (UsersIn.js, UsersOut.js). See the constructor object for the actually loading:
PHP Code:Ext.define('app.store.Users', {
extend: 'Ext.data.Store',
model: 'app.model.User',
sorters: 'lastName',
autoLoad: true,
getGroupString: function(record) {
return record.get('lastName')[0];
},
constructor: function () {
this.callParent(arguments);
// Load the sub stores from this one based on the data loaded.
this.on('load', function (store, records, successful, operation, opts) {
var inStore = Ext.getStore('UsersIn');
var outStore = Ext.getStore('UsersOut');
inStore.removeAll();
outStore.removeAll();
for (var i=0; i<records.length; i++) {
if (records[i].data.clockedIn != '')
inStore.add(records[i]);
else
outStore.add(records[i]);
}
}, this);
}
});
-
29 Feb 2012 6:08 AM #6
Any other way to do that ? Can a 2 views have the same store instance and each one different store filter ?
-
29 Feb 2012 6:12 AM #7Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
29 Feb 2012 6:13 AM #8
Not that I've seen. Because views are essentially stateless in ST, a change to a store when one view is visible would technically also reflect in the view that isn't visible. If both of those views don't show at the same time, you could always refilter the store on show, but if both are visible at the same time, then you might be out of luck and would need to use a solution like I mentioned earlier.
But if anybody else knows of a better way, by all means please pipe in, I'd love to know!
-
29 Feb 2012 6:23 AM #9Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,599
- Vote Rating
- 434
- Answers
- 3102
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.


Reply With Quote