PDA

View Full Version : how to write/update/save json file which is read from proxy in extjs store



please
22 Aug 2012, 2:21 AM
This is my code


Ext.require([
'Ext.form.*'
//'Ext.layout.container.Column',
//'Ext.tab.Panel'
//'*'
]);

Ext.onReady(function() {
Ext.QuickTips.init();

var bd = Ext.getBody();


bd.createChild({tag: 'h2', html: 'Form 1 - Very Simple'});

var required = '<span style="color:red;font-weight:bold" data-qtip="Required">*</span>';

Ext.define('app.formStore', {
extend: 'Ext.data.Model',
fields: [
{name: 'countryid'},
{name: 'countryid1'},

]
});

var myStore = Ext.create('Ext.data.Store', {
model: 'app.formStore',
proxy: {
type: 'ajax',
url : 'org.json',
reader:{
type:'json',
getResponseData: function(response) {
var data, error;

try {
data = response.responseText || '';
if(data.match(/^[^{]*(\{.*\})[^}]*$/)){
data = RegExp.$1;
}
data = Ext.decode(data);
return this.readRecords(data);
} catch (ex) {
error = new Ext.data.ResultSet({
total : 0,
count : 0,
records: [],
success: false,
message: ex.message
});
this.fireEvent('exception', this, response, error);
Ext.Logger.warn('Unable to parse the JSON returned by the server');
return error;
}
}


}
},
autoLoad:true,
listeners: {
load: function() {
//var form = Ext.getCmp('loginForm');
//alert(myStore.data);
//for(var i in myStore.data)
//console.log(i+'=='+myStore.data[i]);

//form.loadRecord(myStore.data.first());

/* var form = Ext.getCmp('loginForm');
form.loadRecord(myStore.data.first());*/

var form = Ext.getCmp('loginForm');
form.getComponent('countryid').setValue( myStore.proxy.reader.countryid);
form.getComponent('countrycode').setValue( myStore.proxy.reader.jsonData.countrycode);

form.loadRecord(myStore.data.first());
}

}});


var testForm = Ext.create('Ext.form.Panel', {
width: 500,
height:250,
renderTo: Ext.getBody(),
title: 'Country Form',
// model:'app.formStore',
id:'loginForm',
store:myStore,
waitMsgTarget: true,
fieldDefaults: {
labelAlign: 'right',
labelWidth: 85,
msgTarget: 'side'
},
//items: [{

// xtype: 'fieldset',
items: [

{
xtype:'textfield',
fieldLabel: 'CODE',
id:'countryid',
hidden:false

}, {
xtype:'textfield',
fieldLabel: 'CODE',
id: 'countrycode'
}, {
xtype:'textfield',
fieldLabel: 'COUNTRY',
id: 'countryname'
}],
// }],
buttons:[
{
items:'button',
text:'save'
},
{
items:'button',
text:'cancel'
},

]

});





//this.testForm.getForm().loadRecord(app.formStore);
});

json is org.json
{"countryid":1,"countrycode":"US","countryname":"United States"}

if i edit in country id field that should be retrieved to to format of json and update in json when i click save button same for cancel also i didnt written clear of buttons please let me if anybody knows

scottmartin
27 Aug 2012, 2:36 PM
Where do you want to write/save the json file to?

Scott.

deepak.jagyasi
28 Jul 2016, 10:07 PM
Can we write the json on local data.json file on clientside?

Say On click of a button I want to persist data on local user machine in a.json file, can that be achieved using any config ?