1

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

Aviv
  • 193
  • 2
  • 11
  • May you provide fiddle example? – Arthur Rubens Aug 05 '20 at 20:43
  • Hey Arthur, @ArthurRubens, thank you for your reply. I'm trying to figure out a way to create an alternative for getType, so I don't know what can I provide in a fiddle. Can you please elaborate? Thanks! – Aviv Aug 05 '20 at 21:12
  • Just realize the problem and write a small fiddle, I will try to fix it. – Arthur Rubens Aug 05 '20 at 21:14
  • @ArthurRubens The problem is that there is not actual alternative to the .getType() method of an association, the only thing I can send you to understand the problem is the Model source code from Sencha Touch 2.4, which is here: https://docs.sencha.com/touch/2.4/2.4.2-apidocs/source/Model.html . My solution to this problem is a bad solution of storing the relations myself, which might cause errors and is not scalable at all. – Aviv Aug 06 '20 at 09:32

1 Answers1

2

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';
    }
});
incutonez
  • 3,241
  • 9
  • 43
  • 92