alt
alt

Reputation: 2616

How do I create an ExtJS 4 Model with multiple belongsTo the same model type?

I'm fairly new to ExtJS and am struggling with it a little.

I have a "Person" model which has a couple of relationships to the same model (Mother and Father).

I'm not clear on how to access these relationships though. Here's what I've got:

Ext.define('IwiDb.model.Person', {
extend: 'Ext.data.Model',
fields: [ 
    'ID',
    'Title',
    'FirstName',
    'MiddleNames',
    'Surname',
    //.. snip
    'MotherID',
    'FatherID',
],
associations: [{
    type: 'belongsTo',
    model: 'IwiDb.model.Person',
    primaryKey: 'ID',
    foreignKey: 'MotherID',
    autoLoad: true,
    name: 'Mother',
    getterName: 'getMother',
},{
    type: 'belongsTo',
    model: 'IwiDb.model.Person',
    primaryKey: 'ID',
    foreignKey: 'FatherID',
    autoLoad: true,
    name: 'Father',
    getterName: 'getFather',
}],

idProperty: 'ID',
proxy: {
    type: 'rest',
    url: 'api/v1.extjs/Person',
    format: 'json',
    reader: {
        type: 'json',
        root: 'items',
        totalProperty: 'totalSize',
    },
    writer: {
        type: 'json',
    },
    simpleSortMode: true,
}
});

EDIT: I think I'm getting close, I've updated the code to what I've got now. At least now I can do person.getMother() and get a function back. But I still can't get the data. Anyone know how?

Upvotes: 3

Views: 4316

Answers (1)

alt
alt

Reputation: 2616

Well, I figured it in the end. The (edited) code above was the correct definition, then if I wanted to eg load the mother, I do something like this:

var mother;
person.getMother(function(result, operation) { mother = result });

Upvotes: 2

Related Questions