2014-11-25 31 views
2

我有一个问题。我创建了一个简单模型,并尝试通过ajax请求使用它来保存新值。但是必须为空的参数发送默认值。你可以通过下面的链接看到它。该代码没有专门设置正确的方式,因为控制台(f12)可以看到下降的挑战。在它中,我通过查询字符串,以及通过有效载荷请求传递一个值(还没有发明如何摆脱它,据我了解 - 默认使用有效载荷)。一般来说,而不是一个空的carId呼叫转移车-1。ajax请求中的Extjs5模型默认值

https://fiddle.sencha.com/#fiddle/dsj

如何解决这种行为,这样做,如果我们不同意任何意义,它通过空的?

+0

[是否有任何方法来禁用模式“idProperty”在EXTJS?] [1] [1]:HTTP ://stackoverflow.com/questions/29562263/is-there-any-way-to-disable-idproperty-of-model-in-extjs – ajokn 2015-04-15 09:18:49

回答

1

您可以创建扩展Ext.data.proxy.Ajax,然后覆盖buildRequest方法来检查所有create行动,并期望值赋给idProperty

Ext.define('CarProxy', { 
    extend: 'Ext.data.proxy.Ajax', 

    alias: 'proxy.carproxy', 
    type: 'ajax', 
    idParam: 'carId', 
    reader: { 
     type: 'json', 
     rootProperty: 'data' 
    }, 
    api: { 
     create: './createcar.json' 
    }, 
    writer: { 
     type: 'form' 
    }, 

    buildRequest: function(operation) { 
     var request = this.callParent(arguments); 
     if (request.getAction() === 'create') { 
      request.getRecords().forEach(function(record) { 
       record.set('carId', ''); //assing desired value to id 
      }); 
     } 

     return request; 
    } 
}); 
0

这是默认的ExtJS的行为,您的自定义代理类。如果你不指定id,它会被生成。为了避免你可以添加构造函数到你的模型:

Ext.define("Car", { 
    [...], 

    constructor: function() { 
     this.callParent(arguments); 
     // check if it is new record 
     if (this.phantom) { 
      // delete generated id 
      delete this.data[this.idProperty]; 
     } 
    } 
}); 
0

谢谢大家谁回答。我想展示我的解决方案:

添加到模型定义参数 标识符:'custom'。 然后创建适当的标识符,其将返回对生成方法未定义:

Ext.define('Custom', { 
    extend: 'Ext.data.identifier.Generator', 
    alias: 'data.identifier.custom', 

    generate: function() { 
     return; 
    } 
});