Hope you are doing good.
I am new in extjs4 and working on sample example of association.
I have 2 different class User and Diploma
In relation, I have defined.
User hasMany Diplomas, and Diplomas belongsTo User
My models are as belows
User.js
Ext.define('MyApp.model.User', {
extend : 'Ext.data.Model',
idProperty : 'userId',
fields : [{
name : 'userId',
type : 'integer'
}, {
name : 'userName',
type : 'string',
}],
hasMany : {
model : 'MyApp.model.Diploma',
name : 'diplomas',
},
});
Diploma.js
Ext.define('MyApp.model.Diploma', {
extend : 'Ext.data.Model',
idProperty : 'diplomaId',
fields : [{
name : 'diplomaId',
type : 'integer'
}, {
name : 'diplomaName',
type : 'string',
}, {
name : 'userId',
type : 'integer'
}],
associations : [{
model : 'MyApp.model.User',
name : 'user',
type : 'belongsTo'
}]
});
=====================
My Stores are as below.========================
Diploma.js
Ext.define('MyApp.store.Diploma', {
extend : 'Ext.data.Store',
model : 'MyApp.model.Diploma',
autoLoad : true,
listeners : {
'load' : {
fn : function(store, records, success, operation) {
console.log('Diplomas are loaded');
}
}
}
});
User.js
Ext.define('MyApp.store.User', {
extend : 'Ext.data.Store',
model : 'MyApp.model.User',
requires:['Ext.data.proxy.LocalStorage'],
autoLoad : true,
listeners : {
'load' : {
fn : function(store, records, success, operation) {
console.log('User is loaded');
}
}
},
data : [{
userId : 1,
userName : 'Rishabh Shah',
diplomas:{
userId : 1,
diplomaId:1,
diplomaName:'IT'
}
}, {
userId : 2,
userName : 'Rakesh Shah',
diplomas:{
userId : 2,
diplomaId:2,
diplomaName:'CE'
}
}, {
userId : 3,
userName : 'Rashmin Shah'
}],
proxy:{
type:'memory',
reader:{
root:'data'
}
}
});
I can get diploma from user like that and can get result
Ext.getStore("User").getAt(0).diplomasStore.getAt(0)
but, when I try to do like
Ext.getStrore("Diploma") or Ext.getStrore("Diploma").getAt(0)
I don’t get anything. items contains nothing…
How can I get user from diploma.
and One more thing….if I don’t write proxy in my Diploma model..
it gives me an error like this..
Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have not supplied it with a url.
Please guide me.
Thanks.
Thanks for your reply..@john..I can get diplomas from User..
I wanted to get user from diplomas…I tried it myself..and found solution…
I will have following json.
[
{
"depId": 1,
"depName": "CE",
"employees": [
{
"empId": 1,
"firstName": "Rishabh",
"lastName": "Shah"
}, {
"empId": 2,
"firstName": "Smit",
"lastName": "Patel"
}
]
}, {
"depId": 2,
"depName": "IT",
"employees": [
{
"empId": 1,
"firstName": "Parag",
"lastName": "Raval"
}, {
"empId": 2,
"firstName": "Rakesh",
"lastName": "Prajapati"
}
]
}, {
"depId": 3,
"depName": "EE",
"employees": [
{
"empId": 1,
"firstName": "Parag EE",
"lastName": "Raval EE"
}, {
"empId": 2,
"firstName": "Rakesh EE",
"lastName": "Prajapati EE"
}
]
}, {
"depId": 4,
"depName": "EC",
"employees": [
{
"empId": 1,
"firstName": "Parag EC",
"lastName": "Raval EC"
}, {
"empId": 2,
"firstName": "Rakesh EC",
"lastName": "Prajapati EC"
}
]
}, {
"depId": 5,
"depName": "Chemial E",
"employees": [
{
"empId": 1,
"firstName": "Parag",
"lastName": "Raval"
}, {
"empId": 2,
"firstName": "Rakesh",
"lastName": "Prajapati"
}
]
}, {
"depId": 6,
"depName": "ME",
"employees": [
{
"empId": 1,
"firstName": "Parag",
"lastName": "Raval"
}, {
"empId": 2,
"firstName": "Rakesh",
"lastName": "Prajapati"
}
]
}, {
"depId": 7,
"depName": "Civil Engineer",
"employees": [
{
"empId": 1,
"firstName": "Parag",
"lastName": "Raval"
}, {
"empId": 2,
"firstName": "Rakesh",
"lastName": "Prajapati"
}
]
}
]
and I will use model.getAssociatedData().user as per my requirement
where model is my record of child class and user is my parent class..
and I was using proxy in my Diploma Model which was not requierd….so, was getting
Ext.data.proxy.Server.buildUrl(): You are using a ServerProxy but have not supplied it with a url.
above mentioned error.
Thanks All ! 🙂
What exactly isn’t working? Have you been able to use the magic methods e.g.,
User.diplomas()? As @VDP stated, the store created for the Association is a normalExt.data.Storescoped to the set of data for the parent model (in this case, User). That is to say, if you have aUsermodel with theidof1, calling the methodUser.diplomas()will return aExt.data.Storefiltered to contain onlyDiploma‘s with auser_idof1.This is very important to understand! If you do not specify the
foreignKeyproperty in ahasManyassociation, it will default to the lowercased name of the owner model plus “_id”, You should amend yourUsermodel’shasManyassociation as so:Perhaps you will find this blog post useful? http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html
Some important tips from the blog post (in case the link dies at some point)
have a very good reason not to *
relationships. **
response as the parent
hasMany to work
*The store will inherit its model’s proxy, and you can always override it
**To make it easy, and avoid potential circular references, you can require them in app.js
Credit: Neil McGuigan (http://www.blogger.com/profile/14122981831780837323)