Results 1 to 5 of 5

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

  1. #1
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
      0  

    Default 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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    You won't be able to get the associated data this way then.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
      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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    You would need to execute getData(false) for each model and follow the association if you need that data.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  5. #5
    Sencha User dewil_de's Avatar
    Join Date
    Mar 2012
    Posts
    28
    Vote Rating
    3
      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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •