PDA

View Full Version : [4.x] mapping feature not used when writing (different from Ext 3)



mankz
3 Aug 2011, 11:50 PM
Consider this simple Ext 3 store.



Ext.onReady(function() {
var store = new Ext.data.Store({
fields : [{name : 'foo', mapping : 'bar'}],
autoSave : true,
proxy : new Ext.data.HttpProxy({
disableCaching : false,
api: {
create: 'test'
}
}),
writer : new Ext.data.JsonWriter({writeAllFields : true})
});

store.loadData([{bar : 'hey' }]);

store.getAt(0).set('foo', 'asd');
});


Resulting POST:


"{"bar":"asd"}"


Same code for Ext 4:


Ext.define('mymodel', {
extend : 'Ext.data.Model',
fields : [{name : 'foo', mapping : 'bar'}]
})

var store = new Ext.data.JsonStore({
model : 'mymodel',
autoSync : true,
proxy : {
type : 'ajax',
api: {
create: 'test'
}
}
});
store.loadData([{bar : 'hey' }]);

store.getAt(0).set('foo', 'asd');


POST:


{"foo":"asd"}


Is this expected? How would you setup your store/writer to write using the mapped field name?

mberrie
11 Aug 2011, 9:28 AM
It's actually all in the docs for Ext.data.writer.Json!! (and I might say that I'm quite a bit surprised ;))

But why the nameProperty is not set to 'mapping' my default I do not know (see below).

Also consider that loadData does *not* respect field mappings and root property config, it expects the data to be in the correct structure already (see class docs for Ext.data.Store, 'inline data') - the code below demonstrates that.

Btw Ext.data.JsonStore is deprecated, just use Ext.data.Store.





Ext.define('MyModel', {
extend : 'Ext.data.Model',
fields : [
{name : 'foo', mapping : 'bar'}
]
})

var store = Ext.create('Ext.data.Store', {
model : 'MyModel',
autoSync : true,
proxy : {
type : 'ajax',
api: {
create: 'json/fail.json'
},
writer: {
type: 'json',
writeAllFields: true,
nameProperty: 'mapping'
}
}
});

var m2 = {
bar: 'old'
}
// loadData expects data to have the correct structure already!
// No field mapping, no root property mapping
store.loadData([m2]);
// the bar property from the input is not correctly mapped!
console.log(store.getAt(0).data); // {bar: 'old', foo: 'asd'}

store.getAt(0).set('foo', 'asd');

// post still works, the rogue property is being ignored (and not sent)
// POST: { bar: 'asd' }



var m1 = {
foo: 'old'
};

store.loadData([m1]);
store.getAt(0).set('foo', 'asd');

// POST: {bar: 'asd'}