extjs中store获取后台数据_如何获取ExtJS JsonStore将JSON数据直接放入请求体?

bd96500e110b49cbb3cd949968f18be7.png

I have a JsonStore configured like so:

var store = new Ext.data.JsonStore({

restful: true,

url: '/categories',

remoteSort: true,

idProperty: 'Id',

totalProperty: 'total',

root: 'results',

writer: new Ext.data.JsonWriter({

encode: false

}),

fields: [ 'Id', 'Name' ]

});

I grab some data from the server, then edit one of the records. When I tell the store to save, it sends this JSON back to the server:

{

"results":

{

"Name":"Trivial123",

"Id":2

}

}

The store is wrapping the JSON inside the results property (the root property configured on the store). However, the server expects this:

{

"Name":"Trivial123",

"Id":2

}

In other words, the serialized entity should be put directly in the response body, and not wrapped in a property. Does anyone know how I can configure the store to do this?

解决方案

You need to override the data rendering function in the JsonWriter, like so:

var rootlessRenderFunction = function (params, baseParams, data) {

if (this.encode === true) {

Ext.apply(params, baseParams);

params = Ext.encode(data);

} else {

params.jsonData = data;

}

};

var myWriter = new Ext.data.JsonWriter({

encode: false,

writeAllFields: true

});

myWriter.render = rootlessRenderFunction;

var myStore = new Ext.data.JsonStore({

// ... various config values ...

writer: myWriter

});

This "rootlessRenderFunction" implementation is the same as the Ext JsonWriter's render code except it doesn't interpose a root in the request data.

This is a hack, of course.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值