Results 1 to 8 of 8

Thread: Trying to understand associations, help please?

  1. #1
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    17

    Default 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.

  2. #2
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,257
    Answers
    759

    Default

    The names are auto-generated by the association system. Assuming you have a schema namespace of MyApp.model, then the call would be:

    Code:
    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.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  3. #3
    Sencha User razvanioan's Avatar
    Join Date
    Feb 2008
    Location
    Romania
    Posts
    135
    Answers
    4

    Default

    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 ?
    ---
    Razvan Ioan ANASTASESCU
    Senior WEB Developer

  4. #4
    Sencha User razvanioan's Avatar
    Join Date
    Feb 2008
    Location
    Romania
    Posts
    135
    Answers
    4

    Default

    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...
    ---
    Razvan Ioan ANASTASESCU
    Senior WEB Developer

  5. #5
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    17

    Default

    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?

  6. #6
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,257
    Answers
    759

    Default

    What does your server return? What do your model definitions look like now?
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  7. #7
    Sencha Premium Member
    Join Date
    Nov 2011
    Posts
    17

    Default

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

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

  8. #8
    Sencha Premium User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,257
    Answers
    759

    Default

    Would suggest you read this guide regarding namespacing: http://docs.sencha.com/extjs/5.1/cor...a_package.html, your current usage isn't correct.

    Also post a sample of the data your server returns.
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

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
  •