Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExtJs4 Model with user defined id

I have the following model:

Ext.define('Gst.model.Order', {
    extend: 'Ext.data.Model',
    idProperty: 'id',
    fields: [
        { name: 'id', type: 'string' } 
    ]
});     

My issue is that the ID of the order model is user defined. So the code to create a new order instance and insert it into the orders store would be:

order = Ext.create('Gst.model.Order', {'id': 'someuserdefinedvalue'}); 
store.insert(0, order);

What happens is because the idProperty is supplied by the user then the order is not marked as phantom and is therefore not considered dirty. Because of this no POST is issued to the server. If I manually set the order to phantom and dirty then I can trigger the POST when I insert the order into the store like:

order = Ext.create('Gst.model.Order', {'id': 'someuserdefinedID'});
order.phantom = true;
order.setDirty(); 
store.insert(0, order);

This would be fine, but my server side code uses REST controllers and expect a POST not to have and id in the url. So while the request extJS issues is

POST http://app.local/api/order/someuserdefinedID.json

While the format my server would look for would be

POST http://app.local/api/order.json

So the only way I have some up with for fixing this would be to create an order and set phantom = true and call setDirty(). This would cause the POST to be issued, and then in my RestProxy override buildUrl: with logic to test for a POST Request and remove the idProperty from the url.

This just seems like an ugly work around to me, and would like to know if there is a better way?

I guess my question is: What is the best way to handle something like this?

like image 715
John Avatar asked Sep 19 '12 04:09

John


1 Answers

Would a possible solution be to add an order_id property to the model and leave the id as the natural key? It seems to me like an awful lot of work to fight against the way the framework wants to treat the idProperty. Just my 2c.

like image 122
John Hall Avatar answered Sep 30 '22 11:09

John Hall