extjssencha-touchextjs6extjs6-modernsenchatouch-2.4

Extjs 6.5 - finding an association type (hasOne, hasMany, belongsTo, etc)


I upgraded my application from Sencha Touch 2.4 to ExtJs 6.5.3.

On Sencha Touch 2.4, there is a function for the Association (Ext.data.association.Association) by the name of getType() that returns the association-type as a string, for example: "hasOne".

as seen for example, in here (the Model source code of Sencha Touch 2.4):

for (i = 0; i < associationCount; i++) {
        association = associations[i];
        associationName = association.getName();
        type = association.getType();
        allow = true;

        if (associationType) {
            allow = type == associationType;
        }

        if (allow && type.toLowerCase() == 'hasmany') {

In my project, by understanding if the association type is hasmany, hasone or belongsto I can "choose" what type of SQL query to create (not originally written by me, but that's a large project and I can't contact the original developer), so that's a must for me.

I try to look into the Extjs 6 / 6.5 documentation and couldn't find anything. Seems like it was deprecated a long time ago.

I was thinking about inserting the "type" inside the model as an object of models and types, for example:

{ 
  'MyProject.model.Receipt': 'hasone',
  'MyProject.model.Order': 'hasmany',
  'MyProject.model.invoice': 'belongsto',
  ...
}

and then try to access it from the model itself and find the type by the association "parent" model.

It feels like a risk and an overkill for such a (suppose-to-be) easy task.

I tried to also find online for solutions but it looks like no one ever tried to find a solution for something like that.

Thank you


Solution

  • The associations property on a record is a bit tricky and a little obscured from us as developers. If you use the inverse property in your associations, they'll sometimes appear in the associations property, which is problematic. I've raised an issue with Sencha Support asking for a reliable method on returning the actual associations on a record, but I've also come up with an interim solution to determine if they're truly an association. I've extended this idea to maybe something that might help you with the types? But you'll have to test this out to determine if it actually works for you... I only use hasOne and hasMany in my code, so I don't know if this is right for belongsTo.

    Here's the Fiddle, and here's the code:

    Ext.override(Ext.data.Model, {
        isActualAssociation: function (role) {
            var isThing = !role.instanceName || role.fromSingle;
            if (!isThing) {
                console.log('found weirdo', role)
            }
            return isThing;
        },
    
        getAssociationType: function (association) {
            if (association.fromSingle) {
                return 'hasOne';
            }
            else if (association.storeName) {
                return 'hasMany';
            }
            return 'belongsTo';
        }
    });