1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    13
    Vote Rating
    0
    bennyl is on a distinguished road

      0  

    Default Answered: associations: no auto generated getters

    Answered: associations: no auto generated getters


    Hi,
    i tried to follow the documentation about association and build the following models
    Code:
    Ext.define('Soa.model.Post', {
        extend: 'Ext.data.Model',
        
        proxy: {
            type: 'rest',
            url : '/data/posts/',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
        
        fields: ["id", "title", "pubDate", "body", "posted"],
    
    
        belongsTo: { model: 'Soa.model.User', name: 'author' }
    });
    
    Ext.define('Soa.model.User', {
        extend: 'Ext.data.Model',
            
        proxy: {
            type: 'rest',
            url : '/data/users/',
            reader: {
                type: 'json',
                root: 'users'
            }
        },
        
        fields: ["id", "name"],
    
    
        hasMany: "Soa.model.Post"
    });
    then i load some posts for example by doing:

    Code:
    var posts = Ext.create('Ext.data.Store', {        
    model: 'Soa.model.Post'});
    posts.load();
    var p = posts.getAt(0);
    console.log(p.getAuthor());
    and the result is that p.getAuthor is not defined,
    but if i call
    Code:
    console.log(p.data.body);
    i received the correct data.

    what am i doing wrong?

  2. You need to do the check after the store has loaded. Also, I added a couple things to the belongsTo to get it to work:

    Code:
    Ext.define('Soa.model.Post', {
        extend: 'Ext.data.Model',
    
        proxy: {
            type: 'rest',
            url : '/data/posts/',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
    
        fields: ["id", "title", "pubDate", "body", "posted"],
    
        belongsTo: { model: 'Soa.model.User', name: 'author', associationKey : 'author', getterName : 'getAuthor' }
    });
    
    var posts = Ext.create('Ext.data.Store', {
        model: 'Soa.model.Post'
    });
    posts.load(function(recs) {
        var rec    = recs[0];
    
        console.log(rec);
        console.log(rec.getAuthor());
    });

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,754
    Answers
    3462
    Vote Rating
    827
    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


    Does the response have an author field to get the belongsTo 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.

  4. #3
    Sencha User
    Join Date
    Dec 2011
    Posts
    13
    Vote Rating
    0
    bennyl is on a distinguished road

      0  

    Default


    yes - here is the response

    Code:
    {
        "posts":[{
            "posted":false,
            "title":"this is the test title",
            "author":{
                "name":"user1",
                "posts":[],
                "id":1
            },
            "pubDate":"Feb 17, 2012 6:21:35 AM",
            "body":"this is a test",
            "id":1
        }],
        "success":true
    }

  5. #4
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,754
    Answers
    3462
    Vote Rating
    827
    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 need to do the check after the store has loaded. Also, I added a couple things to the belongsTo to get it to work:

    Code:
    Ext.define('Soa.model.Post', {
        extend: 'Ext.data.Model',
    
        proxy: {
            type: 'rest',
            url : '/data/posts/',
            reader: {
                type: 'json',
                root: 'posts'
            }
        },
    
        fields: ["id", "title", "pubDate", "body", "posted"],
    
        belongsTo: { model: 'Soa.model.User', name: 'author', associationKey : 'author', getterName : 'getAuthor' }
    });
    
    var posts = Ext.create('Ext.data.Store', {
        model: 'Soa.model.Post'
    });
    posts.load(function(recs) {
        var rec    = recs[0];
    
        console.log(rec);
        console.log(rec.getAuthor());
    });
    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.

  6. #5
    Sencha User
    Join Date
    Dec 2011
    Posts
    13
    Vote Rating
    0
    bennyl is on a distinguished road

      0  

    Default


    thank you for your help,
    that gotten me one step closer but,

    when i do:
    Code:
    rec.getAuthor().get("name");
    this is still undefined - the class name is indeed the "Soa.model.User" but this seem to be no data on it
    so how do i get the user data?

  7. #6
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,754
    Answers
    3462
    Vote Rating
    827
    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


    Have you taken a look at what getAuthor returns?
    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.

  8. #7
    Sencha User
    Join Date
    Dec 2011
    Posts
    13
    Vote Rating
    0
    bennyl is on a distinguished road

      0  

    Default


    yes,
    i looked at the $className field and it value is "Soa.model.User" this why i expect it to have the user fields - like name and id - am i missing something?

  9. #8
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,754
    Answers
    3462
    Vote Rating
    827
    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


    Well that's odd. And the data has the name property?
    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.

  10. #9
    Sencha User
    Join Date
    Dec 2011
    Posts
    13
    Vote Rating
    0
    bennyl is on a distinguished road

      0  

    Default


    rec.getAuthor().data is undefined
    rec.getAuthor().name is : "" (the empty string)
    and rec.getAuthor().getName() is "Soa.model.User"

    strange...

Thread Participants: 1

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..."