i am working in extjs4 MVC and I have been getting stuck at a point which is how to send object array in a single request.
I know how to send single object to server.
1)Here is my some controller code :-
check:function () {
console.log("Inside check function.");
//creating objects in javascript
var obj = new Object();
for (var i = 0; i < 4; i++) {
var inputs = document.getElementsByName(i);
var radio = "";
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].checked) {
name = inputs[j].name;
value = inputs[j].value;
//obj[i].name1=name;
obj[i] = {'questionId': name, 'option': value};
console.log("questionId=" + name + " value=" + value);
console.log("object name=" + obj[i].questionId + " Object value=" + obj[i].option);
var check = Ext.ModelManager.create(
{
questionId: name,
option: value,
}, 'Balaee.model.qb.QbquestionoptionModel');
console.log("User Infooooooooo:" + check.get('option'));
}// End of if statment
}// End of inner for loop
}//End of outer for loop
var storeObject = this.getStore('qb.QbquestionoptionStore');
storeObject.sync();
console.log("data send");
}// End of check function
2)Model class :—
Ext.define('Balaee.model.qb.QbquestionoptionModel',{
extend: 'Ext.data.Model',
idproperty:'',//fields property first position pk.
fields: ['optionId','questionId','isAnswer','option','media','keyword','mediaTypeId',],
/*associations:[
{type:'BelongsTo', model:'Mediatype', foreignKey:'mediaTypeId'},
{type:'BelongsTo', model:'Qbquestion', foreignKey:'questionId'},
{type:'HasMany', model:'Qbregistereduserfreequestionawnser', foreignKey:'answerOptionId'},
]*/
});
3)Here is my store :—
Ext.define('Balaee.store.qb.QbquestionoptionStore',{
extend: 'Ext.data.Store',
model: 'Balaee.model.qb.QbquestionoptionModel',
//autoLoad: true,
proxy:
{
type:'ajax',
api:
{
read:'http://localhost/balaee/Balaee/index.php?r=QuestionBank/qbpaper/SetUserAnswer11',
create: 'http://localhost/balaee/Balaee/index.php?r=QuestionBank/qbpaper/SetUserAnswer12',
update: 'http://localhost/balaee/Balaee/index.php?r=QuestionBank/qbpaper/SetUserAnswer13',
//destroy: ,
},//End of api
reader:
{
type:'json',
//root: ,
//successProperty: ,
},//End of reader
writer:
{
type:'json',
root:'data'
}
}//End of proxy
});//End of store
how can I solve this? please give some suggestion….
Well, first you have to send it as JSON (at least I recommend that) and that can easily be done cause the
Ext.Ajax.request()method supports this out of the boxAnd if you want to do it with a store use the type
autofor the Modelfield. This type can contain any object. Here’s a JSFiddle with a list of objects within a model.Here is a modification of your store configuration. Note that you can either define a
readonly API or a CRUD API. For the later it is not possible to leave f.e. thedestroyaction unset. You also need to know that a root property within thewriterimplies that you want to encode your data, meaning all get send viagetwhich is defiantly not what you want.Now to what this proxy does: Per default
batchwill betruewhich would cause the store to submit all changes at once when sync get called. If there are more then one changes for a action the proxy will submit a array of objects instead of a single one. Now if you don’t take care about this you would now need to predict if you get a object or a array of objects for each request. We don’t want end up in something like this. But there is theallowSingleproperty on the writer which get us around this. If you set it totrueit will always send a array of objects back to the server even if there is only one. You now know that you get a array each time.