Results 1 to 10 of 10

Thread: Very Basic Model Associations - 6.6.0 Classic

  1. #1
    Sencha Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default Very Basic Model Associations - 6.6.0 Classic

    I am struggling to get model associations to work. I have a very simple example set up, but it is not working in the way that I expect.

    Let's say we have two simple models for mouths and teeth. There is a many to one relationship here. Mouths can have many teeth, but a tooth can only be in one mouth.

    I have a fiddle set up here to express this relationship with a keyed association. Teeth can have a reference to a mouth. The data is coming from two separate json files, one for mouths, and one for teeth.

    In the fiddle, you will see that I have 3 teeth and 2 mouths. Two teeth belong to one mouth, and the other tooth belongs to the other mouth.



    I expect the first mouth to return a store containing those two teeth when I call its generated storeGetter method (.teeth() in this case). However, it returns a store containing all three of the teeth, even though only two reference this mouth. Why is this?

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

    Default

    It's expecting you to filter the records server side. It will send the mouth_id as part of the request to load teeth.
    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 Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default

    Thanks for the quick response. That makes sense. I see now that it is sending a filter parameter in the GET request.

    filter: [{"property":"mouth_id","value":153375,"exactMatch":true}]

    Is there a way to customize this behavior, i.e., to prevent the filter parameter being sent, and to instead simply send the mouth_id as a parameter in the GET request that comes from mouth.teeth()?

  4. #4
    Sencha Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default

    I apologize if I seem impatient, but can anyone help me with this?

    Calling mouth.teeth() sends a request to the server. By default, it sends the parameter
    filter: [{"property":"mouth_id","value":153375,"exactMatch":true}]
    in the request to the URL configured in the Tooth model. How can I customize this request?

    How would I:
    1. Remove the filter parameter from the request?
    2. Add the parameter
    mouth_id: 153375
    to the request?
    3. Build the the request URL to be '/mouth/153375/teeth'?

    Note: I don't want to do all of these three things at once, but I would like to know how to do each of them.
    Any partial answers or suggestions, or just pointing me in the right direction, would be greatly appreciated.

  5. #5
    Sencha User dongryphon's Avatar
    Join Date
    Jul 2009
    Location
    Kansas
    Posts
    1,748
    Answers
    55

    Default

    On the /mouth/153375/teeth approach you should check out the Rest proxy.

    As for sending a header in the request (at least I think that is what you are asking in #2), I would definitely avoid that since that would mean all your URL's look the same but are in fact requesting different things.

    Unless you mean ?mouth_id=153375 parameter in which case you are talking about a custom proxy.
    Don Griffin

    "Use the source, Luke!"

  6. #6
    Sencha Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default

    Wow, two of the greats have replied to me in the same thread! Cool!

    Thank you very much for the help, Don! I was able to get this to work using a custom Rest proxy with the buildUrl method (see below), but I feel that my solution ended up being a bit hacky. Also, I was not able to figure out how to remove the ?filter=[{"property":"mouth_id","value":153375,"exactMatch": true}] parameter. I extract the mouth_id from this unneeded filter parameter, and then I manually build the url. Again, this is working for me, but is there a better, proper way to do this? My proxy is as follows:

    Code:
    Ext.define('MyFiddle.proxy.ToothProxy', {
        extend: 'Ext.data.proxy.Rest',
        alias: 'proxy.tooth',
    
        requires: [
            'Ext.data.reader.Json'
    ],
    
        reader: {
            type: 'json'
    },
        appendId: false,
        buildUrl: function (request) {
            var me = this;
            var filter = Ext.JSON.decode(request.getParams().filter)[0];
            var mouthId = filter.value;
            var url = '/mouth/' + mouthId + '/teeth';
            me.setUrl(url);
            return me.callParent([request]);
        },
    });

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

    Default

    If you configure your proxy with filterParam: '', it won't send attach any filters to the request (but you will still be able to access them via the operation).
    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.

  8. #8
    Sencha Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default

    Thank you again Evan! That worked very well. The filter parameter is no longer being sent with the request, but I am still able to access my mouthId from within my ToothProxy's buildUrl method by using:
    Code:
    var mouthId = request.getOperation().getFilters()[0].getConfig().value;
    Still, I worry that this is a hacky approach. Is this really the best way to get my mouthId to use in buildUrl? Is there a simpler way to access mouthId other than extracting it from a buried filter?

    Thanks again for all the help! I really do appreciate it, and I'm sure if any people have the same problem as me and read this in the future, then they will too.

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

    Default

    Unfortunately there isn't. The teeth store does have a reference to the mouth record, however it doesn't forward that down the chain (proxy/operation), so there's no good way to retrieve it.

    I certainly think it would be useful for the store to poke that information on as it performs a load.
    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.

  10. #10
    Sencha Premium User
    Join Date
    Jun 2018
    Posts
    21

    Default

    Ah, I see. Well, I agree that that would be useful, as shown in my case here. Thank you again for all your help!

Similar Threads

  1. [FIXED] [6.0.2] examples/classic/data/associations demo crashes
    By mankz in forum Ext JS 6.x Bugs
    Replies: 1
    Last Post: 3 May 2016, 9:13 AM
  2. Basic nested model associations/references problem
    By simberman in forum Ext 5: Q&A
    Replies: 4
    Last Post: 11 Nov 2015, 11:20 AM
  3. First Project : Basic > MVC (Basic-Model/View/Controller architecture)
    By volk3r in forum Sencha Architect 3.x: Bugs
    Replies: 2
    Last Post: 26 Jan 2014, 1:53 AM
  4. [DUP] [2.1] Due to the Model.cache, associations aren't updated when reloading a model
    By Steffen Hiller in forum Sencha Touch 2.x: Bugs
    Replies: 2
    Last Post: 22 Aug 2012, 7:42 AM
  5. Replies: 1
    Last Post: 13 Feb 2012, 12:48 AM

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
  •