PDA

View Full Version : Trying to understand associations, help please?



SSzretter
7 Feb 2015, 6:46 AM
I have spent quite a while researching associations / references, but am not having much luck actually getting them to work in my SA project. I have two models/stores:


Programs
id
name

Attachments
id
programId
text

The models have ajax proxies, that load JSON data from a php server. The stores are set to auto load, and do this when the application runs. This all works fine, and my stores program grid populates.

I have an program detail view (form), that opens from the programs grid and loads up a program record. On that view I have a button, that I want to load a new view / grid to list the attachments for that program. SO, I have tried two things -- I first tried adding a 'reference' in the attachments store to "Programs" (Many attachments for one program). I have also tried adding the legacy association "hasMany" like this:

Tried reference on the programId field of the attachment model:

fields: [
{
name: 'id'
},
{
name: 'programId',
reference: 'MyApp.model.ProgramModel'
}
],





Also tried legacy 'hasMany' in the Programs model:

hasMany: {
model: 'MyApp.model.Attachment',
foreignKey: 'programId'
}



NOW, where I am stuck is actually figuring out HOW to get the associated / related records. From the click event code of the button, I am trying to do something like this:


var detailView = button.up('form'); //get the view this button is on
var form = detailView.getForm(); // get the actual form object
var record = form.getRecord(); // put the record out
// ******** AT THIS POINT, I DO NOT KNOW HOW TO GET MY RELATED ATTACHMENTS RECORDS ********
// I do not see any getters for attachments, for example. I can see in the sencha debug that the attachments store is loaded (loaded at application launch) but cant figure out how to actually get at the related list?????



NOTE - in case it's important, this is the basic code I use when the row is clicked to load up the Program detail form:



var store = Ext.getStore('ProgramStore'),
record = store.getById(selected[0].data.id); //load record from store
var detailView = Ext.widget('programDetailForm');
var form = detailView.getForm();
form.loadRecord(record); // load record in to form




Your help is appreciated. Thanks.

evant
8 Feb 2015, 12:02 AM
The names are auto-generated by the association system. Assuming you have a schema namespace of MyApp.model, then the call would be:



var store = record.attachments(); // Returns a store


Depending on whether you've provided the data when loading, the store may or may not be populated.

razvanioan
8 Feb 2015, 12:07 AM
and if the data is not sent together with parent records (performance reasons and so on), accessing them like this will trigger the auto load of the filtered child records using it's proxy ?

razvanioan
8 Feb 2015, 12:45 AM
and another question, how would I access parent record from a child one ?

if I try childRecord.getParent() this triggers loading parent record form the server, which I don't want as it's there, already loaded in the store !

in my example I have employees each of them having multiple plans, so from a plan record I used: planRecord.getEmployee()

I want to get the employee record data from employees store where it's already loaded

I'll keep digging...

SSzretter
8 Feb 2015, 4:26 PM
Ok, so I was missing the schema. Now that I have added that, I have my method and am getting a store. However, I am not seeing any data in the store returned by this method (even though the base store has all the records and a matching record via programId.

Do I have to do anything else (such as a refresh/load/filter of some kind), or should I have data this point?

evant
8 Feb 2015, 5:25 PM
What does your server return? What do your model definitions look like now?

SSzretter
9 Feb 2015, 3:53 AM
During the initial application load, I am seeing the model proxies send their requests and the stores load up. I do not see anything happen, when I call that attachments function.

On strange thing, I do not have a method called 'attachments()', I end up with a method named "record.programProgramsMyAppModelAttachments()". This is the method I am trying to call, and it looks like it is an attachment store, but the data property contains no data.

The difference in my code, I added a schema:


Ext.define('ProgramsMyApp.model.ProgramSchema', {
extend: 'Ext.data.schema.Schema',
alias: 'schema.programschema'
});

Ext.define('ProgramsMyApp.model.ProgramModel', {
extend: 'Ext.data.Model',


requires: [
'ProgramsMyApp.model.ProgramSchema',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json',
'Ext.data.field.Date'
],


schema: 'programschema',


fields: [
{
name: 'id'
},
{
name: 'name'
}
],


proxy: {
type: 'ajax',
url: 'j/mprx.php',
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST',
destroy: 'POST'
},
reader: {
type: 'json',
rootProperty: 'r'
}
}
});

Ext.define('ProgramsMyApp.model.Attachment', {
extend: 'Ext.data.Model',


requires: [
'ProgramsMyApp.model.ProgramSchema',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json',
'Ext.data.field.Date'
],
uses: [
'Programs508T.model.ProgramModel'
],


schema: 'programschema',


fields: [
{
name: 'id'
},
{
name: 'name'
},
{
name: 'programId',
reference: 'ProgramsMyApp.model.ProgramModel'
}
],


proxy: {
type: 'ajax',
url: 'j/mprxPA.php',
actionMethods: {
create: 'POST',
read: 'POST',
update: 'POST',
destroy: 'POST'
},
reader: {
type: 'json',
rootProperty: 'r'
}
}
});







On the 'detail' form for a program, I have a button and in the click event am trying to get the related list of records so I can open a new view/grid and pass them to it:



var detailView = button.up('form');
var form = detailView.getForm();
var record = form.getRecord();


var records = record.programMyAppModelProgramAttachments(); //am seeing nothing in the data[] property/object here
// once I have records would open a new grid and pass them to it here

evant
9 Feb 2015, 4:33 AM
Would suggest you read this guide regarding namespacing: http://docs.sencha.com/extjs/5.1/core_concepts/data_package.html, your current usage isn't correct.

Also post a sample of the data your server returns.