1. #1
    Sencha User ykey's Avatar
    Join Date
    Mar 2010
    Location
    USA
    Posts
    245
    Vote Rating
    27
    ykey has a spectacular aura about ykey has a spectacular aura about

      2  

    Default Answered: Why doesn't Ext.create('Model', ...) load associated model data?

    Answered: Why doesn't Ext.create('Model', ...) load associated model data?


    I am having trouble loading associations through Ext.create. The following works fine when data is retrieved through a server proxy. However when the same data is loaded through Ext.create no associations are created.

    The UI component depends on the nested stores and I would like to have default data available if the AJAX call fails or times out and for testing purposes.

    Code:
    Ext.define('Server', {
            extend: 'Ext.data.Model',
            idProperty: 'name',
            fields: [
                'name'
            ],
            
            hasMany: {
                model: 'Container', 
                name: 'containers'
            }
        });
        
        Ext.define('Container', {
            extend: 'Ext.data.Model',
            idProperty: 'name',
            fields: [
                'name'
            ]
        });        
        
        var sys = Ext.create('Server', {
            "name": "Server 1",
            "containers": [
                {
                    "name": "A"
                },
                {
                    "name": "B"
                },
                {
                    "name": "C"
                }
            ]
        });
        
        console.log(sys); // Has 3 containers in the data field
        console.log(sys.containers());  // Has 0 records

  2. I've got this on my list of ExtJS 4.0 issues. I believe 4.1 addresses a lot of the shortcomings of 4.0 so I'm waiting for that release before I throw in a 'feature request' for this.

  3. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,489
    Answers
    526
    Vote Rating
    283
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    I agree that what you're trying to do appears sensible but it doesn't work. My guess is that associations are handled at the reader level and using Ext.create() skips that. It seems very counter-intuitive to me.

    It took me quite a while to find a way of doing it that actually worked. Maybe there's a better way but this is what I came up with:

    Code:
    var reader = Ext.create('Ext.data.reader.Json', {
        model: 'Server'
    });
    
    var resultSet = reader.read({
        "name": "Server 1",
        "containers": [
            ...
        ]
    });
    
    var sys = resultSet.records[0];
    All feels a bit yucky to me. I don't see anything there that couldn't be done implicitly in the model's constructor.

  4. #3
    Sencha User
    Join Date
    Oct 2010
    Posts
    73
    Answers
    2
    Vote Rating
    4
    devnullable is on a distinguished road

      0  

    Default


    I feel your pain guys. I was just wondering what's going on when associations are not loaded on Ext.create. Working on anything more "complex" than in examples, like a form with grids of associated model data is really frustrating experience. Build JSON by hand for saving primary model AND association models in same atomic AJAX request and then try to figure out how to load back response again...

  5. #4
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,489
    Answers
    526
    Vote Rating
    283
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    I've got this on my list of ExtJS 4.0 issues. I believe 4.1 addresses a lot of the shortcomings of 4.0 so I'm waiting for that release before I throw in a 'feature request' for this.

  6. #5
    Sencha User mberrie's Avatar
    Join Date
    Feb 2011
    Location
    Bangkok, Thailand
    Posts
    506
    Answers
    26
    Vote Rating
    14
    mberrie will become famous soon enough mberrie will become famous soon enough

      0  

    Default


    Is there a bug report for this? I wouldn't expect 4.1 to fix an issue just because it is obvious that a feature is incomplete.

  7. #6
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,489
    Answers
    526
    Vote Rating
    283
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    I've seen comments from the Sencha devs that 4.1 will improve the data model, particularly associations. I don't know the details but I can't be bothered spending hours filing reports on issues until I know what's in 4.1.

    I think the word bug may be a little strong in this case.

  8. #7
    Ext JS Premium Member Sébastien.Volle's Avatar
    Join Date
    Apr 2011
    Location
    Grenoble, France
    Posts
    70
    Vote Rating
    0
    Sébastien.Volle is on a distinguished road

      0  

    Default


    From what I've seen in 4.1 RC3, Ext.create('Model', ...) or new Model(...) still does not create association data in the Model instance. One still have to go through a reader for the associated data to be built.

    Is there any plan for adding that in 4.1 GA? When instanciating a model instance, when all the required data is provided, I don't see why association data should not be there in the instance.
    aka Seboss

  9. #8
    Sencha User
    Join Date
    May 2009
    Posts
    8
    Vote Rating
    0
    gjuggler is on a distinguished road

      0  

    Default


    I've started running into this problem too.

    It seems silly that associations can be loaded from serialized formats via the JSON and XML reader classes, but not from an in-memory data structure via the standard Ext.create() call. It's an unfortunate oversight in the API design.

    I'd love to see this fixed in a future release!

  10. #9
    Sencha User
    Join Date
    Jan 2012
    Posts
    26
    Vote Rating
    0
    dougi is on a distinguished road

      0  

    Default


    i thought first that it was a bug on my side but it's worst. Can we expect a correction soon?

  11. #10