具有用户定义ID的ExtJs4模型 [英] ExtJs4 Model with user defined id

查看:106
本文介绍了具有用户定义ID的ExtJs4模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

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

我的问题是订单模型的ID是用户定义的。因此,创建新的订单实例并将其插入到订单存储中的代码将是:

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

发生的是因为idProperty是由用户提供的,那么订单没有被标记为幻影,是因此不算脏。因为这样没有POST被发送到服务器。如果我手动将订单设置为幻影和脏,那么当我将订单插入商店时,我可以触发POST:

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

这很好,但是我的服务器端代码使用REST控制器,并期望POST不具有id在url。所以当请求extJS问题是

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

虽然我的服务器将寻找的格式是

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

所以唯一的办法就是创建一个命令并设置phantom = true并调用setDirty()。这将导致POST被发出,然后在我的RestProxy中覆盖buildUrl:具有逻辑来测试POST请求并从URL中删除idProperty。



这只是好像是一个丑陋的工作给我,想知道是否有更好的方法?



我想我的问题是:处理这样的最好的方法是什么?

解决方案一个可能的解决方案是将一个 order_id 属性添加到模型中,并留下 id 作为自然的钥匙?在我看来,像框架想要对待 idProperty 的方式来打击的方式是非常大的工作。只是我的2c。


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?

解决方案

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.

这篇关于具有用户定义ID的ExtJs4模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆