3
votes

ExtJS Model fields have mapping option.

fields: [
        {name: 'brandId', mapping:'brand.id',   type: 'int'},
        {name: 'brandName', mapping:'brand.name', type: 'string'},

The problem is: if the response from server does not contain some field(brand field in my example) and mapping from inner fields is defined, Ext Store silently fails to load any records.

Does anybody have problems with this? Is it some kind of a bug?

UPDATE To make it clear: suppose I have ten fields in my model. Response from server has nine fields, one is missing. If there is no nested mapping for this field (mapping:'x.y.z') everything is OK - store loads record, the field is empty. But if this field has to be loaded from some nested field and has mapping option - store fails to load ANYTHING.

UPDATE 2 I have found the code, that causes problems. The fact is: when Ext tries to load some field from Json it performs a check like this

(source["id"] === undefined) ? __field0.defaultValue : source["id"]

But when field has mapping option(mapping 'brand.id') Reader does it this way

(source.brand.id === undefined) ? __field20.defaultValue : source.brand.id

which causes error if source has no brand field.

In case you have same problems as I: you can fix it by overloading Ext.data.reader.Json's method createFieldAccessExpression

2
What other behaviour would you expect if the store fails to find it's record in the response? I mean, what is your issue with this behaviour?Jean-Francois Hamelin
Well, the record comes from server, it just doesn't have "brand" field. If a record misses some field and there is no mapping, everything is OK, the store loads record, the field is emptymik
"(brand field in my example)" You don't have a brand field shown here. You have a brandId and brandName. Please clarify.Jere
There is no brand field in model, but there should be brand field in server response "brand":{"id":"1","name":"Hyundai"} Am I clear?mik
"The Store silently fails to load any records" is just because it can not determine what the actual data of brand in the server (brand is not received in the response, so how can the store get brand.id and brand.name). And if brand is not determined, how the store know whether this field has been changed comparing to actual data on server in order to update. So event handling in this case will be impossible.U and me

2 Answers

2
votes

I agree that Ext should only fail to load that field, not the entire record. One option that isn't great, but should work, is instead use a mapping function:

{
    name: 'brandId',
    mapping: function(data, record) {
        return data.brand && data.brand.id;
    }
}

I could have the arguments wrong (I figured out that this feature existed by looking at the source code), so maybe put a breakpoint in there to see what's available if it doesn't work like this.

0
votes

I think you're misinterpret mapping and nesting paradigms: these are not interchangeable. If you define nesting in your data, the result MUST have the corresponding field.