1. #1
    Sencha User
    Join Date
    Jan 2013
    Location
    Luxembourg
    Posts
    4
    Vote Rating
    0
    serge.schumacher is on a distinguished road

      0  

    Default Unanswered: Associations

    Unanswered: Associations


    Hi,

    I know that there a tons of threads about Associations but I could find one who solved my problem.
    I have a json file which contains all my info, now I want to map the different subparts to their corresponding model.
    When loading the store I don't get a reference to "restaurants".

    Can someone help me to sort this out ?

    Regards,
    Serge

    I have this json print :
    Code:
    {
    • _id: {
      • _time: 1376427165,
      • _machine: -276415190,
      • _inc: -496027987,
      • _new: true
      },
    • contactFirstName: "John",
    • contactLastName: "Doe",
    • contactEmail: "jd@taxo.lu",
    • restaurants: [
      • {
        • name: "IL Destino",
        • email: "resto1@taxo.lu",
        • cartes: [
          • {
            • name: "Carte 1",
            • diches: [
              • {
                • name: "Pizza Jambon",
                • category: "Pizza",
                • price: 14.1,
                • priceCurrency: "€",
                • kalories: 200,
                • active: true
                },
              • {
                • price: 0,
                • priceCurrency: "€",
                • kalories: 0
                },
              • {
                • price: 0,
                • priceCurrency: "€",
                • kalories: 0
                },
              • {
                • price: 0,
                • priceCurrency: "€",
                • kalories: 0
                }
              ]
            }
          ]
        }
      ]
    }
    Code:
    Ext.define('FOOD.model.customer', {
        extend: 'Ext.data.Model',
        id: 'customer',
        fields: [
            'pid',
            {name: 'contactFirstName'},
            {name: 'contactLastName'},
            {name: 'contactEmail'}
    
    
        ],
        proxy: {
            type: "ajax",
            method: 'POST',
            reader: {
                type: 'json'
            },
            writer: {
                type: 'json'
            },
            api: {
                read  : CONFIG.BackendURL + '/api/getCustomer?id=1'
            }
        },
        hasMany : {
            associationKey  : 'restaurants',
            model           : 'FOOD.model.restaurant',
            name            : 'restaurants'
    
    
        }
    });
    Code:
    Ext.define('FOOD.model.restaurant', {
        extend: 'Ext.data.Model',
        id: 'restaurant',
        fields: [
            'pid',
            {name: 'name'},
            {name: 'streetName'},
            {name: 'houseNr'},
            {name: 'zipCode'},
            {name: 'telNumber'},
            {name: 'faxNumber'},
            {name: 'email'},
            {name: 'gpsCoordinations'}
    
    
        ],
        associations:[
            { type:'belongsTo', model:'FOOD.model.customer', name:'customer'}
        ]
    });
    Code:
    Ext.define('FOOD.store.customers', {
        extend: 'Ext.data.Store',
        model: 'FOOD.model.customer',
        storeId: 'customers',
        autoLoad: true,
        autoSync: false,
    
    
        listeners: {
            load: function (store, records, success) {
                console.log("customers load: success " + success);
                console.log(store);
    
    
    
    
                var restos = records.restaurants;  --> DOES NOT EXIST
                debugger;
                store.each(function (r) {
    
    
    
    
                })
    
    
    
    
    
    
            },
            update: function (store, record, operation, eOpts) {
                switch (operation) {
                    case Ext.data.Model.EDIT:
                        console.log('INFO', 'Updating record...');
                        break;
                    case Ext.data.Model.COMMIT:
                        console.log('INFO', 'Record was updated!');
                        break;
                    case Ext.data.Model.REJECT:
                        console.log('ERR', 'Something went horribly wrong :( Data was rejected!');
                        break;
                }
            },
            write: function (store, operation, eOpts) {
                console.log('write');
            }
    
    
        }
    });

  2. #2
    Sencha Premium Member
    Join Date
    Jun 2008
    Posts
    326
    Vote Rating
    13
    Scorpie will become famous soon enough

      0  

    Default


    Put a proxy on the model.
    I`m from Holland!

  3. #3
    Sencha Premium Member dawesi's Avatar
    Join Date
    Mar 2007
    Location
    Melbourne, Australia (aka GMT+10)
    Posts
    1,089
    Vote Rating
    52
    Answers
    25
    dawesi has a spectacular aura about dawesi has a spectacular aura about

      0  

    Default


    in your proxy, put a root in your reader:

    (your other reader/writer code is unnecessary as ajax proxy defaults to json reader/writer.)

    Code:
    proxy : {
        type : 'ajax',
        method : 'post',
        url : CONFIG.BackendURL + '/api/getCustomer?id=1'
        reader : {
            root : 'restaurants'
            }
        }
    A couple of best practices also:
    • Your application name should be mixed case (aka Food instead of FOOD, or FoodApp instead of FOODAPP if you get the gist) same with CONFIG
    • BackendURL should be backendUrl (eg: Food.Config.backendUrl )
    • Also why is CONFIG a global variable? It could be Food.Config.backendUrl, while you can get away with this, unless you're sharing the config with other apps, it's better inside the application as best practice. so then your file would be /app/Config.js using this reference.
    Enjoy.
    Lead Trainer / Sencha Specialist
    Teahouse Training Company

    Comprehensive Javascript, Sencha and 3rd Party Stack Training available worldwide online, and onsite in APAC, written by certified official Sencha trainers.



    Unofficial Sites:

    SenchaWorld.com - Videos, and stuff
    Sencha.Community - Tools, Extensions and Resources
    TeahouseHQ.com - Sencha Ecosystem Training Portal

    Code Validation : JSLint | JSONLint | JSONPLint

Thread Participants: 2

Tags for this Thread