PDA

View Full Version : Store - Supress Specific Actions



Bleak
8 Oct 2010, 5:30 AM
I have a store associated to a gridpanel, and I want to be able to suppress calls to the create action based on my own criteria, but if there are other actions such as update/destroy, etc I would like them to continue.

So, in short, is it possible to tell a store to save but exclude any create calls?

Foster
8 Oct 2010, 5:33 AM
In your store instead of just setting the URL, use the Proxy with something like
ProxyConfig = {
api : { read : {url: yourURL, method: 'GET' } }
}
etc
Under the api you can explicitly set the different operations, so you could null out the create and delete


I have a store associated to a gridpanel, and I want to be able to suppress calls to the create action based on my own criteria, but if there are other actions such as update/destroy, etc I would like them to continue.

So, in short, is it possible to tell a store to save but exclude any create calls?

Condor
8 Oct 2010, 5:44 AM
You could create your Record class with an isValid method that returns false for phantom records (invalid fields are never saved).

Bleak
8 Oct 2010, 5:48 AM
Hey Foster,

I do use a proxy, however, I want to be able to turn the create proxy on/off as necessary. I've tried;



this.dao.getProxy().setApi(Ext.data.Api.actions.create, null);
The dao class has the following method


//The dao getProxy() Method is defined as;
getProxy: function() {

if (! this.proxy) {
this.proxy = new Ext.data.HttpProxy({
api: {
read :'pp.do?xaction=read',
create: 'pp.do?xaction=create',
update: 'pp.do?xaction=update',
destroy: 'pp.do?xaction=destroy'
}
});
}

return this.proxy;
}

But if I attempt to set the create call to null, it just throughs an error. The idea is to be able to turn the create call on/off as necessary based on criteria.

Is there a correct way to change the proxy settings dynamically?

Condor
8 Oct 2010, 6:00 AM
Any specific reason you want to disable 'create' requests?

Couldn't you modify the GUI so the user isn't able to insert records when no allowed?

Bleak
8 Oct 2010, 6:07 AM
This is stemming from always having an extra record in the grid for the purpose of the user being able to enter a new record right from the editor grid. There are a couple of columns that are required by the backend to allow the record to be created and these require user input. Right now, the create method is called with no data, which the back end handles appropriately but it just seems like a wasted call entirely since the record is blank.

What I've done, is added an event that is triggered before the store calls save and it checks to see if there are any records pending for create, and if the required fields are not populated, I want the create call to be skipped, but any outstanding update/destroy requests to continue through.

Foster
8 Oct 2010, 6:11 AM
Hey Dean,

I tried to find an example in my app where I did something similar but couldnt find one. I know I never explicitly set any part of the api config to null after instantiation, I just left off the declarations in my initial definition, and it ends up set to undefined.

This might not be the most elegant fix, but can you try:

Add a beforeWrite listener to the store itself, which should get passed the proxy and the action, and if you return false there it should abort the create. You could potentially use logic there, or check if the api.create is null and abort, otherwise perform the create.

-Foster

Condor
8 Oct 2010, 6:16 AM
As I said, handle this by making the record with emtpy data invalid.

Marking one or multiple fields with allowBlank:false will take care of that.

Bleak
8 Oct 2010, 6:27 AM
Condor, thanks! I can't believe I didn't think to look at the record definition to specify required fields. This worked, perfectly.

Foster, the beforeWrite listener also worked, thanks for the approach.

Bleak
8 Oct 2010, 7:10 AM
Hey Condor,

Bit off topic, but whats the best method for determining if a phantom record exists in a store? I iterate over the records right now checking each records phantom attribute, but thinking there must be a proper way?

Condor
8 Oct 2010, 7:29 AM
No, that is the only way.