Results 1 to 4 of 4

Thread: Extjs accessing father from child belongsTo relationship

  1. #1
    Sencha User
    Join Date
    Nov 2013
    Posts
    5
    Answers
    1
    Vote Rating
    0
      0  

    Default Answered: Extjs accessing father from child belongsTo relationship

    I need a little help, I've been struggling with this for days! : I'm trying to access the father from a child in a belongsTo relationship, I've tried everything, every example, and nothing! what am I doing wrong, here is my code:
    Father:

    Code:
    Ext.define('MyApp.model.Career', {     
       extend: 'Ext.data.Model' 
       ,idProperty: 'CareerID' 
       ,fields: ['Name', 'SchoolID', 
           {            name : 'CareerID',            type: 'int'        } 
       ]    
       ,proxy: {        
           type: 'ajax'                
           ,url: 'fakeServices/careers.js'                
            ,reader: {           
                type: 'json',            
                 root: 'data',           
                  successProperty: 'success'        
              }    
        }
    });

    Child:


    Code:
    Ext.define('MyApp.model.Course', {     
     extend: 'Ext.data.Model'    
    ,fields: ['CourseID', 'Name', 'CourseStatusID', 'Description',        
      {            name: 'CareerFK',            type: 'int'        }
     ]    
    ,belongsTo: {                    
      model:'Career',        
      primaryKey: 'CareerID',        
      foreignKey: 'CareerFK',        
      associationKey: 'Career'    
    }
     ,proxy: 
     {        
        type: 'ajax'        
       ,url: 'fakeServices/courses.js'        
       ,reader: {            
         type: 'json',            
         root: 'data',            
         successProperty: 'success'        
       }    
     }
    });

    How I'm loading (Courses is a simple Store with no proxy, just declaring the Course Model):


    Code:
    Ext.StoreManager.lookup('Courses').load();

    Json:


    Code:
    {
      "success" : true, 
      "data" : [{ 
           "CourseID": 1,            
          "Name": "Curso1",            
          "CourseStatusID": "1",           
          "Description": "Learn!", 
          "CareerFK" : 1,            
          "Career": {            
             "CareerID" : 1,                
             "Name": "Engineering"            
           }    
      }]
    }

    What I'm specting (getCareer method should exist, but it doesn't, all the fields in the Course Model have the JSON data loaded correctly):


    Code:
    Ext.StoreManager.lookup('Courses').load();
    Ext.StoreManager.lookup('Courses').data.items[0].getCareer();

    PS: I've tried to replacing belogsTo for associations:[{type: 'belongsTo'}], it still doesn't work

  2. I only changed the model property to model's full name:

    Code:
    model:'MyApp.model.Career'
    And Voila!

  3. #2
    Sencha User
    Join Date
    Sep 2011
    Posts
    574
    Answers
    59
    Vote Rating
    74
      0  

    Default

    Take a look on MyApp.model.Course.prototype in a dev tool. I believe you'll find two quite interesting methods there.
    If you want to change their names try 'getterName'&'setterName' in association config.

  4. #3
    Sencha User
    Join Date
    Nov 2013
    Posts
    5
    Answers
    1
    Vote Rating
    0
      0  

    Default Thanks for the answer. But I don't see anything interesting in the prototype.

    This is what I see in the prototype:

    • $className: "MyApp.model.Course"
    • HasListeners: function () {}
    • associations: constructor
    • config: objectClass
    • configMap: Object
    • fields: constructor
    • idField: constructor
    • initConfigList: Array[0]
    • initConfigMap: Object
    • modelName: "MyApp.model.Course"
    • proxy: constructor
    • self: function constructor() {
    • superclass: Object
    • validations: Array[0]
    • __proto__: Object

  5. #4
    Sencha User
    Join Date
    Nov 2013
    Posts
    5
    Answers
    1
    Vote Rating
    0
      0  

    Default Got it working!

    I only changed the model property to model's full name:

    Code:
    model:'MyApp.model.Career'
    And Voila!

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
  •