PDA

View Full Version : Extjs accessing father from child belongsTo relationship



arielalvarez88
30 Nov 2013, 6:58 AM
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:



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:



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):


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

Json:



{
"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):


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

ettavolt
30 Nov 2013, 11:38 AM
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.

arielalvarez88
1 Dec 2013, 9:26 AM
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

arielalvarez88
1 Dec 2013, 10:04 AM
I only changed the model property to model's full name:


model:'MyApp.model.Career'

And Voila!