I am not getting the proper JSON for sending server request from the viewModel. I want to create a view model for user registration with mutiple addresses. For that I have created a viewModel.
Here is my ViewModel code
---------------------------------------------------------------------------------------------------
Ext.define('MyApp.view.sample.UserAddressViewModel', {
extend : 'Ext.app.ViewModel',
alias : 'viewmodel.userAddressViewModel',
requires : ['MyApp.view.sample.UserModelBase','MyApp.view.sample.UserAddressModel','MyApp.view.sample.UserModel'],
model : 'UserModel',
fields : [{
name : 'fname',
type : 'string'
},{
name : 'lanme',
type : 'string'
}]
stores : {
address : {
model : 'UserAddressModel',
data : []
}
},
});
---------------------------------------------------------------------------------------------------
Here is my Model code
---------------------------------------------------------------------------------------------------
Ext.define('MyApp.view.sample.UserAddressModel', {
extend : 'MyApp.view.sample.UserModelBase',
fields : [ {
name : 'addressType',
type : 'string'
}, {
name : 'address',
type : 'string'
}, {
name : 'city',
type : 'string'
}, {
name : 'state',
type : 'string'
}, {
name : 'country',
type : 'string'
} ]
});
---------------------------------------------------------------------------------------------------
And here is my View
---------------------------------------------------------------------------------------------------
Ext.define('MyApp.view.sample.UserAddress', {
extend : 'Ext.form.Panel',
title : 'Form Panel',
requires : [ 'MyApp.view.sample.UserAddressViewModel',
'MyApp.view.fw.FormModel',
'MyApp.view.sample.UserAddressController' ],
viewModel : {
type : 'userAddressViewModel'
},
controller : 'userAddressController',
items : [ {
xtype : 'textfield',
fieldLabel : 'First Name',
name : 'fname',
bind : '{fname}',
itemId : 'fname'
}, {
xtype : 'textfield',
fieldLabel : 'Last Name',
name : 'lname',
bind : '{lname}'
}, {
xtype : 'form',
itemId : 'addressForm',
title : 'Address Form',
items : [ {
xtype : 'textfield',
fieldLabel : 'Address Type',
name : 'addressType',
}, {
xtype : 'textfield',
fieldLabel : 'Address',
name : 'address',
}, {
xtype : 'textfield',
fieldLabel : 'City',
name : 'city',
}, {
xtype : 'textfield',
fieldLabel : 'state',
name : 'state',
}, {
xtype : 'textfield',
fieldLabel : 'Country',
name : 'country',
}, {
xtype : 'button',
text : '+',
listeners : {
click : 'onAddAdressClick'
}
} ]
}, {
xtype : 'grid',
name : 'address',
itemId : 'addressGrid',
bind : {
store : '{address}'
},
columns : [ {
header : 'AddressType',
dataIndex : 'addressType',
hidden:true
}, {
header : 'Address',
dataIndex : 'address'
}, {
header : 'city',
dataIndex : 'city'
}, {
header : 'state',
dataIndex : 'state'
}, {
header : 'Country',
dataIndex : 'country'
} ],
} ],
buttons : [ {
text : 'Save',
listeners : {
click : 'saveForm'
}
} ]
});
---------------------------------------------------------------------------------------------------
Here is the controller with add record in store and save function
---------------------------------------------------------------------------------------------------
Ext.define('MyApp.view.sample.UserAddressController', {
extend : 'Ext.app.ViewController',
alias : 'controller.userAddressController',
onAddAdressClick : function(el) {
var addressGrid = this.getView().down("#addressGrid");
var addressForm = this.getView().down('#addressForm');
addressGrid.getStore().add(addressForm.getValues());
},
saveForm : function() {
console.log(Ext.JSON.encode(this.getViewModel().getData()));
},
});
---------------------------------------------------------------------------------------------------
As I have binded the viewModel with the view and address grid with the address model, I need the Ext.JSON.encode(view.getViewModel().getData()); as expected json given below,
---------------------------------------------------------------------------------------------------
{
"fname": "asdf",
"lname": "asdf",
"address": [
{
"addressType": "Home",
"address": "abc",
"city": "xyz",
"state": "pqr",
"country": "srk"
},
{
"addressType": "sadf",
"address": "sadf",
"city": "asdf",
"state": "sadf",
"country": "asdf"
}
]
}
---------------------------------------------------------------------------------------------------
Array elemnet in json not working properly.
Please help for the JSON from the ViewModel with the Array elements.
Thanks in advance.
Json Creation problem with View Model
Aucun commentaire:
Enregistrer un commentaire