samedi 7 février 2015

Trying to understand associations, help please?

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:

Code:



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:



Code:



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



Also tried legacy 'hasMany' in the Programs model:


Code:



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:

Code:



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:

Code:



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.



Trying to understand associations, help please?

Aucun commentaire:

Enregistrer un commentaire