1. #1
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
    dewil_de is on a distinguished road

      0  

    Default Unanswered: Model (with has many / belongs to) to json

    Unanswered: Model (with has many / belongs to) to json


    Hey,


    i have a model called Event which has many participants.
    The Participant i defined as, belongs to Event.


    Now i want to export my Event as a Json.
    But when i do Ext.JSON.encode(event.getData()), i get the following error:
    RangeError: Maximum call stack size exceeded.


    I think the problem here is that when i look at the event.getData() object this contains a participants variable, which itself contains an event variable.
    And ofcourse that event variable contains a participants... etc.


    So this goes into an infinite loop.


    Now my question here is, what is the best way to describe my models?
    What associations should i use?


    Code:
    Ext.define('EventManager.model.Event', {
        extend: 'Ext.data.Model',
        config: {
            hasMany: [
                {
                    model: 'EventManager.model.Participant',
                    name: 'participants'
                },
                {
                    model: 'EventManager.model.Cost',
                    name: 'costs'
                }
            ]
        }
    });

    Code:
    Ext.define('EventManager.model.Participant', {
        extend: 'Ext.data.Model',
        config: {
            belongsTo: {
                model: 'EventManager.model.Event'
            }
        }
    });

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,074
    Answers
    3500
    Vote Rating
    854
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You won't be able to get the associated data this way then.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
    dewil_de is on a distinguished road

      0  

    Default


    Yeah, so i was wondering whats the best way to do this?

    is there a way to skip the event object insisde the event.particpants object?

    or should i implement this using some for loops? which i'd rather avoid.

  4. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,074
    Answers
    3500
    Vote Rating
    854
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    You would need to execute getData(false) for each model and follow the association if you need that data.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  5. #5
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
    dewil_de is on a distinguished road

      0  

    Default


    calling getData(false) doesn't suffice.
    when i look at me eventObj.getData(false) the participant array just contains 1 value instead of all the participants.

    At the moment is just do the following

    Code:
    delete eventVO.participants[participantNum].Event;
    delete eventVO.participants[participantNum].event_id;
    But thats not really the clean way i'm looking for.

Thread Participants: 1

Tags for this Thread