Extjs 4 - associations hasMany不在loadData上加载嵌套列表

时间:2013-12-17 12:04:05

标签: javascript extjs extjs4 associations

以下是我的2个关联模型:

Ext.define('NG.model.business.Order', {
extend: 'NG.model.business.AbstractBusiness',
idProperty: 'iD',
requires: [
    'NG.model.business.OrderLine'
],

fields: [
    { name: 'iD', type: 'string' },
    { name: 'hasAttachments', type:'boolean' },
    { name: 'transactionDate', type: 'date', dateFormat: 'c' },
    { name: 'orderNumber', type: 'string' },
    { name: 'vendorSiteID', type: 'int' },
    { name: 'customerSiteID', type: 'int' },
    { name: 'totalAmount', type: 'float' },
    { name: 'isActive', type: 'boolean' }
],

associations: [{
    type: 'hasMany',
    model: 'NG.model.business.OrderLine',
    foreignKey: 'orderArchiveID',
    associationKey: 'orderLines',
    name: 'lines'
}]
});


Ext.define('NG.model.business.OrderLine', {
extend: 'Ext.data.Model',
idProperty: 'iD',
fields: [
    { name: 'iD', type: 'string' },
    { name: 'orderArchiveID', type: 'string' },
    { name: 'orderNumber', type: 'string' },
    { name: 'orderDate', type: 'date', dateFormat: 'c' },
    { name: 'lineNumber', type: 'string' },
    { name: 'catalogNumber', type: 'string' },
    { name: 'customerCatalogNumber', type: 'string' },
    { name: 'itemDescription', type: 'string' },
    { name: 'unitMeasure', type: 'string' },
    { name: 'unitPrice', type: 'string' },
    { name: 'quantity', type: 'string' },
    { name: 'quantityAsNumber', type: 'float' }   
]
});

以下是从我的服务器返回时对象的数据:

{
"iD": "dbc55bc0-2a05-4544-81f7-7378e2c464b0",
"orderEndCustomerName": "מפעל מלמ",
"orderPurchasingGroup": "",
"vendorDuns": "510674039",
"orderLines": [{
    "iD": "0d6fc3f5-5188-4ecc-bd9c-ebefda14950f",
    "orderArchiveID": "dbc55bc0-2a05-4544-81f7-7378e2c464b0",
    "orderNumber": "H31123-012",
    "orderDate": "2013-11-03T00:00:00",
    "lineNumber": "4",
    "catalogNumber": "102079",
    "customerCatalogNumber": "102079",
    "itemDescription": "A4 נייר צילום 75 גר' לבן, לובן 110 - גודל",
    "unitMeasure": "PK",
    "unitPrice": 3.28,
    "quantity": "4",
    "totalAmount": 13.12,
    "currency": "USD",
    "deliveryAddress": "",
    "comments": "",
    "isActive": true,
    "status": "",
    "customerSiteID": 8078,
    "vendorSiteID": 9736,
    "deliverySiteID": "",
    "agreementNumber": "",
    "agreementLineNumber": "",
    "orderLineApprovalState": "Undecided",
    "linkage": "False",
    "vendorComments": "",
    "lineStateID": 1,
    "orderLineDeliveryNotes": [{
        "iD": "641b5383-ea84-4bd1-9a22-7564fb5044b7",
        "orderLineArchiveID": "0d6fc3f5-5188-4ecc-bd9c-ebefda14950f",
        "orderNumber": "H31123-012",
        "quantity": "4",
        "deliveryDate": "2013-11-06T00:00:00"
    }]
}, {
    "iD": "ae10df47-25d7-4bc4-9551-1e36398681c6",
    "orderArchiveID": "dbc55bc0-2a05-4544-81f7-7378e2c464b0",
    "orderNumber": "H31123-012",
    "orderDate": "2013-11-03T00:00:00",
    "lineNumber": "3",
    "catalogNumber": "1370027",
    "customerCatalogNumber": "1370027",
    "itemDescription": "סכין יפני בינוני איכותי גוף מתכת אן טי פרו הץ 2",
    "unitMeasure": "EA",
    "unitPrice": 13.23,
    "quantity": "5",
    "totalAmount": 66.15,
    "currency": "USD",
    "deliveryAddress": "",
    "comments": "",
    "isActive": true,
    "status": "",
    "customerSiteID": 8078,
    "vendorSiteID": 9736,
    "deliverySiteID": "",
    "agreementNumber": "",
    "agreementLineNumber": "",
    "orderLineApprovalState": "Undecided",
    "linkage": "False",
    "vendorComments": "",
    "lineStateID": 1,
    "orderLineDeliveryNotes": [{
        "iD": "400ed534-026e-4ddc-984d-c28fd9b7600d",
        "orderLineArchiveID": "ae10df47-25d7-4bc4-9551-1e36398681c6",
        "orderNumber": "H31123-012",
        "quantity": "5",
        "deliveryDate": "2013-11-06T00:00:00"
    }]
}, {
    "iD": "b03734a8-4770-4d85-9762-abde47202cb7",
    "orderArchiveID": "dbc55bc0-2a05-4544-81f7-7378e2c464b0",
    "orderNumber": "H31123-012",
    "orderDate": "2013-11-03T00:00:00",
    "lineNumber": "2",
    "catalogNumber": "304029",
    "customerCatalogNumber": "304029",
    "itemDescription": "עט סמון פרמננטי עם חוד לבד 90/70 שחור - דגם ארטי- 12 יח' בחבילה",
    "unitMeasure": "PK",
    "unitPrice": 1.69,
    "quantity": "1",
    "totalAmount": 1.69,
    "currency": "USD",
    "deliveryAddress": "",
    "comments": "",
    "isActive": true,
    "status": "",
    "customerSiteID": 8078,
    "vendorSiteID": 9736,
    "deliverySiteID": "",
    "agreementNumber": "",
    "agreementLineNumber": "",
    "orderLineApprovalState": "Undecided",
    "linkage": "False",
    "vendorComments": "",
    "lineStateID": 1,
    "orderLineDeliveryNotes": [{
        "iD": "c7618e93-da05-48e9-9d02-35b35d483c3d",
        "orderLineArchiveID": "b03734a8-4770-4d85-9762-abde47202cb7",
        "orderNumber": "H31123-012",
        "quantity": "1",
        "deliveryDate": "2013-11-06T00:00:00"
    }]
}, {
    "iD": "f7375a44-1af1-48d6-b97a-d4904e8cb2f7",
    "orderArchiveID": "dbc55bc0-2a05-4544-81f7-7378e2c464b0",
    "orderNumber": "H31123-012",
    "orderDate": "2013-11-03T00:00:00",
    "lineNumber": "1",
    "catalogNumber": "603014",
    "customerCatalogNumber": "603014",
    "itemDescription": "סיכות למכונת הידוק 26/6 באריזות של 5000 -דגם ארטי",
    "unitMeasure": "PK",
    "unitPrice": 0.45,
    "quantity": "20",
    "totalAmount": 9.0,
    "currency": "USD",
    "deliveryAddress": "",
    "comments": "",
    "isActive": true,
    "status": "",
    "customerSiteID": 8078,
    "vendorSiteID": 9736,
    "deliverySiteID": "",
    "agreementNumber": "",
    "agreementLineNumber": "",
    "orderLineApprovalState": "Undecided",
    "linkage": "False",
    "vendorComments": "",
    "lineStateID": 1,
    "orderLineDeliveryNotes": [{
        "iD": "baa7a010-af6b-4848-836c-cb00217c5d93",
        "orderLineArchiveID": "f7375a44-1af1-48d6-b97a-d4904e8cb2f7",
        "orderNumber": "H31123-012",
        "quantity": "20",
        "deliveryDate": "2013-11-06T00:00:00"
    }]
}],
"orderStatusName": "Changed",


"year": 2013
}

以下是我加载数据的方式:

orderData = {
    success: true,
    data: [Ext.decode(response.responseText)] // THE RESPONSE IS HOW I WROTE IT ABOVE.
},
order = Ext.create('NG.model.business.Order', orderData),

现在我得到的行有很多关联,但商店总是空的。

该协会没有加载!!!

1 个答案:

答案 0 :(得分:2)

foreignKey选项由代理的读者处理,您在此处绕过。

考虑到您已为模型配置了代理,您可以直接将其阅读器与您的数据一起使用:

var data = Ext.decode(response.responseText),
    reader = NG.model.business.Order.getProxy().getReader(),
    resultSet = reader.readRecords(data),
    // The reader returns a ResultSet, so you must grab your record
    // from it. You should probably ensure that the read operation was
    // successful and returned one record.
    order = resultSet.records[0];

console.log(order.getAssociatedData());
// > Object {lines: Array[4]}