PDA

View Full Version : Grid loadData() callback for selectionModel



jsheely
13 Mar 2014, 6:43 AM
I'm running into a race condition with loadData() and then assigning rows to the grids selectionModel()



myGrid.getView().setLoading(true);
myGrid.store.loadData(data);
myGrid.getView().setLoading(false);
var myGridSelectionModel = myGrid.getSelectionModel();


Ext.each(myGrid.store.data.items, function(record, index) {
if (_.contains(currentSelectionsArray, record.get("Name"))) {
//Event fires but selection is never made. If wrapped in a setTimeout for 1500ms then it works.
myGridSelectionModel.select(index, true);
}
});





I've tried adding the selection code to the grid stores datachanged event but the results are the same.

Is there a better way to handle the selections and get the same callback style as the load({callback: function(){}}).

Thanks

jsheely
13 Mar 2014, 6:57 AM
Alright well I resolved my issue by putting the Ext.each() logic into the grids viewready event.

I'm not sure this is the best way to handle it so I'm still open to any other suggestions.

jsheely
13 Mar 2014, 7:25 AM
Alright it turns out that last fix was silly and only worked on the intial vewready. Forgot that event only fires the first time the view is ready not after the data is loaded.

So my current fix while I think is still a little sloppy but actually works is:

Add viewready: checkForSelection to the grid events
Add datachanged: checkForSelection to the grids store.


var checkForSelection = function () {
if (grid.rendered) {
if (controller.currentSelections != null) {
var gridSelectionModel = Ext.getCmp('myGrid').getSelectionModel();
_.each(Ext.getCmp('myGrid').store.data.items, function(record, i) {
if (_.contains(controller.currentSelections.get("Name"), record.get("Name"))) {
gridSelectionModel.select(i, true);
}
});
}
controller.currentSelections = null;
}
};


I feel like this was a lot more effort than it should be. Seems like a simple callback on loadData() would be a lot more straight forward.

Advice?

firefoxSafari
13 Mar 2014, 7:59 AM
I've solved similar problems maintaining selection state by putting a listener on the store's load event and executing the selection handling from there. Not sure if this is better or worse than the two events you're listening for.

You will have to write some code to maintain selection state. My only advice is that you can subclass the grid panel and possibly the selection model to in order to achieve the custom behavior that you need, attaching listeners to the grid's store in the constructor. That way at least the code is centralized and not duplicated.