relationships

by el_chief

HTML

<link rel="stylesheet" href="//extjs.cachefly.net/ext-4.1.0-gpl/resources/css/ext-all-gray-debug.css" />
<script src="//extjs.cachefly.net/ext-4.1.0-gpl/ext-all-dev.js"></script>

JavaScript

/* this is a function to simulate ajax requests in jsfiddle */

function sim(proxy, data, delay) {
    if (typeof data !== 'string') {
        data = Ext.JSON.encode(data);
    }

    proxy.url = '/echo/json/';
    proxy.actionMethods.read = "POST";
    proxy.extraParams.json = data;
    proxy.extraParams.delay = typeof delay == 'undefined' ? 0 : delay;
}

var data = {
    success: true,
    users: [{
        id: 2,
        username: 'neil',
        email: '[email protected]',
        profile: {
            owner_id: 2,
            website: 'http://extjs-tutorials.blogspot.com/',
            photo: 'http://www.zastavki.com/pictures/1024x768/2010/Men_Very_handsome_man_020715_.jpg'
        },
        posts: [
            {
            id: 66,
            author_id: 2,
            title: 'some bullshit',
            content: 'that no one cares about',
            categories: [{
                id: 3,
                name: 'lies'},
            {
                id: 4,
                name: 'PR nonsense'}]},
        {
            id: 99,
            author_id: 2,
            title: 'some fake ad',
            content: 'to sell you shit you don\'t need',
            categories: [{
                id: 6,
                name: 'truthiness'}]}
        ]}]
};

Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        'id',
        'username',
        'email'
        ],

    hasMany: [
        {
        name: 'posts',
        model: 'Post',
        associationKey: 'posts',
        foreignKey: 'author_id'}
    ],

    hasOne: [
        {
        name: 'profile',
        instanceName: 'profile',
        model: 'Profile',
        associationKey: 'profile',
        foreignKey: 'owner_id'}
    ],
    
    proxy:{
        type:'ajax',
        url:'nullsville',
        reader:{
            type:'json',
            root:'users'
        }
    }
});

Ext.define('Post', {
    extend: 'Ext.data.Model',
    fields: [
        'id',
        'author_id',
        'title',
    ...