ExtJs 4 POST array of records from store - post

I have a simple rest store and after filling it with several records I am trying to send post request to server with array of records by calling create function.
The POST request is sent but only with one record instead of array of records.
UPDATE - I forgot to mention that this record has fields with empty values, and that is strange too, because I am filling store with values.
I stalled. Please give me a light where did I go wrong. Thanks.
My code samples:
Ext.define('my.store.testStore',{
extend: 'Ext.data.Store',
storeId: 'teststore',
model: 'my.model.testModel',
proxy: {
type: 'rest',
url: 'http://someurl.ru',
reader: 'json'
}
});
Ext.define('my.model.testModel',{
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'phone', type: 'string'},
{name: 'email', type: 'string'}
]
});
var namesList = [Ext.create('my.model.testModel',{
'name':'test name1',
'phone':'343-343',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'6345',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'24324',
'email':'test#test.com'
})
];
var testStore = Ext.create('my.store.testStore');
testStore.loadData(namesList);
testStore.create();

You should use testStore.sync() insted of testStore.create()
Another things:
testStore.create() - this function takes an object as first parameter and creates instance of the model then sends it to the server.
In case if "create" is called without parameter then instance of the model is created based on values from fields defaultValue which by default is set to "", that is way you got response with empty values.
About namesList. You forgot to add ")" to the 3-th model.
var namesList = [Ext.create('my.model.testModel',{
'name':'test name1',
'phone':'343-343',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'6345',
'email':'test#test.com'
}),
Ext.create('my.model.testModel',{
'name':'test name2',
'phone':'24324',
'email':'test#test.com'
})//Here was missed ")"
];

After a great time, this issue appeared again so I had to solve it anyway.
And finally I succeeded to find working solution. The point is was just to add batchActions: true to store. Like this:
Ext.define('my.store.testStore',{
extend: 'Ext.data.Store',
storeId: 'teststore',
model: 'my.model.testModel',
proxy: {
type: 'rest',
url: 'http://someurl.ru',
reader: 'json',
batchActions: true
}

Related

Get reference from ext component

I'm having difficulties retrieving my input values through Extjs6's references. There doesn't seem to be a clear cut answer and google is polluted with answers that seem distinct to many different Extjs versions.
I have a window which contains a textfield and a save button, from the textfield I need to retrieve the user's input and pass it along to my ajax call.
My code:
window.updatePassword = function(button) {
var win = new Ext.Window({
referenceHolder: true,
items: [{
xtype: 'form',
items: [{
xtype: 'textfield',
fieldLabel: "newPassword",
reference: 'newPassword',
inputType: 'password'
}],
}],
buttons: [{
text: 'save',
handler: function (btn) {
Ext.Ajax.request({
url: '../../Password_updatePassword.action',
params : {
newPassword: win.newPassword
},
scope: this,
disableCaching: true
});
},
scope: this
}]
});
win.show(this);
};
The things I've tried so far:
this.lookupReference('newPassword')
win.values
win.getValues()
win.newPassword
Ext.getCmp('newPassword')
Any advice would be appreciated.
this.lookupReference('newPassword') - This refers to current object
and handler dont have any component to lookup.
win.values - doesnt make any sense unless you have created a config
in win.
win.getValues() - again doesnt make any sense unless you have create a method in win.
win.newPassword - again same.
Ext.getCmp('newPassword') - getCmp works with id, not with reference.
To get the reference of password field you can lookup on win object,
win.lookupReference('newPassword');
To get the value you have to use getValue() method.
win.lookupReference('newPassword').getValue();

Store subclass does not work in extjs

I have created a class like
Ext.define('abc.StoreService', {
extend: 'Ext.data.Store',
autoLoad: true,
autoSync: true,
proxy: {
type: 'memory',
reader: 'json',
data: [{
date: "2016-07-15",
arrival: 'Foo',
dep: 'abc'
}]
},
}
Ext.define('abc.mystore.Store', {
extend: 'abc.StoreService',
alias: 'Abc Store',
});
But if i use in grid like
store: 'abc.mystore.Store',
or use
store: 'Abc Store',
it does not load the store data. Is there any thing wrong i am doing?
You're not assigning a store id, or registering the store with the StoreManager.
The 'store' parameter in Ext.grid.Panel takes either a store instance (which you could make by doing Ext.create('abc.mystore.Store'), or an id of a store registered in the StoreManager.

extjs 4.2 POST data id=0 generates method not allowed - laravel 4

When I send data ( store in codeblock ) to my laravel 4 server I get "method not allowed" and the server returns all methods allowed except POST. When I comment out 'id' in my model, everything works. ( don't want to comment out id)
I tried the writeRecordId:false and writeAllFields:false in my writer property but this doesn't remove the id while sending..
Ext.define('Equipment.store.Equipments', {
extend: 'Ext.data.Store',
model: 'Equipment.model.Equipment',
requires: ['Ext.data.proxy.Rest'],
alias: 'store.Equipments',
proxy: {
type: 'rest',
url: '/json/stock/equipment',
reader: {
type: 'json',
root: 'data',
successProperty: 'success'
},
writer: {
type: 'json'
}
},
groupField: 'location'
});
data send:
{"id":0, "location":"Building123","locationDetails":"office 2","locationIndex":"drawre 5", "description":"item 7"}
I guess I've sorta solved it I think:
Ext.define('Equipment.model.Equipment', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id',
type: 'number',
useNull: true
},
Placing 'useNull: true' along id sets {"id":null, ... in the data which is accepted by the server. Anyone care to comment?

ExtJS4.1: Why model.save() sends wrong thing to server?

I tried to use model.save() to POST a new user. But I check request payload and found that it not only sent the data, but also sent other parts of the model. That makes my server cannot parse the payload.
The request payload generated :
{"phantom":true,"internalId":"ext-record-58","raw":{},"data":{"userId":0,"userName":"Amy"},"modified":{"userName":""},"hasListeners":{},"events":{},"stores":[],"dirty":true,"id":"AM.model.User-ext-record-58"}
But the desired request payload should be :
{"userId":0,"userName":"Amy"}
And I am aware that the "phantom" of my model is false before I call model.save(). But it becomes true in the request payload. Is it a clue?
Model:
Ext.define('AM.model.User',{
extend: 'Ext.data.Model',
fields: [
{ name: 'userId', type: 'int' },
{ name: 'userName', type: 'string' },
{ name: 'createdTime', type: 'string' },
],
idProperty: 'userId',
associations: [
{
type: 'hasOne',
model: 'AM.model.ModelA',
name:'modelA',
associationKey:'modelA',
getterName:'modelA'
},
{
type: 'hasOne',
model: 'AM.model.ModelB',
name:'modelB',
associationKey:'modelB',
getterName:'modelB'
}
],
proxy: {
type: 'rest',
success:true,
url:'../restful/users',
writer:{
type:'json',
getRecordData:function(record){ //parse createdTime to the format Y-m-d
record.set('createdTime', Ext.Date.format(new Date(record.get('createdTime')), "Y-m-d"));
return record;
}
},
reader: {
type: 'json'
}
}
});
This is the view which has the data to be posted. The view will fill the data to the model:
Ext.define('AM.view.UserRegisterForm',{
extend:'Ext.form.Panel.',
alias:'widget.userRegisterForm',
fields:new Array(), //I want to render the fields in xtemplate, so instead of adding the fields to items, I use an array to manage them.
retrieveData(model){
model.set('userName', this.fields[0].getValue());
model.set('createdTime',this.fields[1].getValue());
}
}
The function in the controller, which sends the POST request:
postUser:function(){
var userRegisterForm= this.getUserRegisterForm();
var userModel = this.getUserModel();
var user= new userModel();
var me = this;
userRegisterForm.retrieveFieldData(user);
console.log(user); //the data in console looks fine!
user.save({
success: function(response) {
//do something...
},failure:function(response) {
alert('fail');
}
});
}
You are returning the full record when you override getRecordData Where as you are just meant to return the records data. record.getData()
Some extra advice. Don't override getRecordData to set the models creation date. Use the models defaultValue property to give assign it a new Date if one doesn't exist.

extjs editor grid update rails

i am trying to update records displayed in editor grid..but instead of updating same record, a new record gets inserted into the database...what am i missing??pllzz help..following is my JsonStore code :
Ext.data.Api.restActions = {
create : 'POST',
read : 'GET',
update : 'PUT',
destroy : 'DELETE' };
ProdStore = Ext.extend(Ext.data.JsonStore, {
constructor: function(cfg) {
cfg = cfg || {};
ProdStore.superclass.constructor.call(this, Ext.apply({
storeId: 'ProdStore',
id:'ProdStore',
url: 'products.json',
root: 'proddata',
restful:true,
idProperty:'id',
successProperty:'success',
autoLoad:true,
autoSave:false,
batch:true,
writer: new Ext.data.JsonWriter({
encode: false,
listful: false,
writeAllFields: false}),
fields: [
{ name:'customer_id'},{ name: 'prodnm'},
{ name: 'qty'}, { name: 'rate' }, { name: 'amt'}
]
}, cfg));
}
});
new ProdStore();
The idProperty set on the store should be the field that represents unique rows in the database. Perhaps customer_id in this case?
If this does not fix the issue, I would have to see the back end code to see how the save is being handled.

Resources