7

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?

sra
  • 23,820
  • 7
  • 55
  • 89
John
  • 465
  • 1
  • 4
  • 17
  • You should provide the exact ExtJS Version you are using – sra Sep 19 '12 at 06:31
  • 1
    It's unusual to set ID on the client side for a new record. That is usually the database's job. – dbrin Sep 19 '12 at 16:55
  • Dmitry: I thought about using an db controlled pk, but it just made more sense, but I am open for suggestions. The above model is to be used in a system to track sales reps commissions in orders they place. Since the actual order system is closed, I figured using its already unique orderId as the pk just made the most sense. – John Sep 20 '12 at 03:55
  • 1
    You have to use a surrogate int id. Some ui components (i.e. grid panel and tree panel) expects an integer id field for selection and other internal stuff. – Diego L Espiñeira Jan 10 '13 at 02:41

2 Answers2

1

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.

John Hall
  • 1,346
  • 13
  • 27
  • I agree with John. Although I'm a data warehouse guru, so I am going to suggest different terms for his suggestion. You should have a surrogate key (unique key) called id, and keep that blank during record creation to avoid an API function call hack with the framework. And then add another property, as the natural key, called order_id. Set order_id during the creation of your record, and have id as the identity id in your database, so it increments by one each time, and doesn't need to be set by a user. A rule of thumb for any app is the end user should never see the internal (unique) id. – JustBeingHelpful Sep 12 '14 at 05:40
0

Like John Hall says, you should either use another idproperty or rename your id so that an implicit id is used by the framework (internal id).

The dirty/phantom would then work and posting would include the user id in data, but not as the key of the record.

adaskos
  • 151
  • 1
  • 7