1. #1
    Ext JS Premium Member
    Join Date
    Jan 2008
    Posts
    50
    Vote Rating
    0
    dewie is on a distinguished road

      0  

    Default Model.load not sending id, loading all models

    Model.load not sending id, loading all models


    I'm having a little trouble getting the hang of loading a single record from a model
    I define a model:

    Code:
    var JobType = Ext.regModel('JobType', {
        fields: [
            {name: 'id', type: 'integer'},
            {name: 'code' , type: 'sting'},
            {name: 'name' , type: 'sting'}
        ],
        proxy: {
            type: 'rest',
            url: '/job_types',
            reader: {
                type: 'json'
            }
    
        }
    });
    When i read the docs right, it should be possible to do :

    Code:
    JobType.load(1200,, {
        success: function(jt) {
            console.log(jt.getId()); //logs 1200
        }
    });
    But the call that gets sent to the webserver is:

    Started GET "/job_types?_dc=1296425984138" for 127.0.0.1 at Sun Jan 30 23:19:44 +0100 2011
    Processing by JobTypesController#index as */*

    The url generated should be GET "/job_types/1200" or am i reading the docs wrong ?

  2. #2
    Sencha User
    Join Date
    Oct 2009
    Location
    Bucharest, Romania
    Posts
    70
    Vote Rating
    0
    icflorescu is on a distinguished road

      0  

    Default Confirmed...

    Confirmed...


    Just wanted to bump this up, confirmed the same behavior...

  3. #3
    Sencha Premium Member
    Join Date
    Sep 2010
    Posts
    3
    Vote Rating
    0
    ianc is on a distinguished road

      0  

    Default


    Me too, any suggestions as to what I am doing wrong?

  4. #4
    Sencha Touch Premium User
    Join Date
    Oct 2010
    Location
    Portland, Maine USA
    Posts
    66
    Vote Rating
    1
    gordonjl is on a distinguished road

      0  

    Default


    Same here with me. Here's my code:

    The Model:
    Code:
    /**
     * @class PublicResource
     */
    Ext.regModel('PublicResource', {
    
        idProperty:'id',
        fields: [
            {name:'id',type:'int'}, 'text', 'description', 'distance', 'latitude', 'longitude','website','category',
            {
                name: 'startDate',
                type: 'date',
                dateFormat: 'c'
            },
            {
                name: 'endDate',
                type: 'date',
                dateFormat: 'c'
            },
            {
                name: "phone",
                type: "string"
            }
        ],
        proxy: {
            type: 'rest',
            url : 'vicinity',
            format:'json',
            reader:{
                type:'json', root:'items'
            }
        }
    
    
    
    });
    The call to load:
    Code:
      var PublicResource = Ext.ModelMgr.getModel('PublicResource');
    
            PublicResource.load(options.recordId, {success:function(resource) {
                log.debug("fetched a record!");
            },failure:function(record, operation) {
                log.debug("failed to load!  ")
            } });
    The issue seems to be in Ex.data.RestProxy.buildUrl(). It looks like the id is only appended if there is a record in the request:
    Code:
        buildUrl: function(request) {
            var records = request.operation.records || [],
                record  = records[0],
                format  = this.format,
                url     = request.url || this.url;
            
            if (this.appendId && record) { // <-- THE ID ISN'T APPENDED IF THERE IS NO RECORD.
                if (!url.match(/\/$/)) {
                    url += '/';
                }
                
                url += record.getId();
            }
            
            if (format) {
                if (!url.match(/\.$/)) {
                    url += '.';
                }
                
                url += format;
            }
            
            request.url = url;
            
            return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments);
        }
    });
    Any clever person come up with a workaround yet?

  5. #5
    Sencha User
    Join Date
    Jun 2010
    Posts
    18
    Vote Rating
    0
    ksystems is on a distinguished road

      0  

    Default ID param passed to model.load not being added to operation.params?

    ID param passed to model.load not being added to operation.params?


    Having had a look at this it seems to me like operation.params is not being set when the load call is processed and the proxy read is being called. I have a config object with action: 'read' and id: <idval>, but the id passed through to the call to load never makes its way down to the operation.params.
    Stepping into the code just before
    Code:
    this.proxy.read(operation, callback, this);
    and setting
    Code:
    operation.params = { id: <idval> }
    then letting the application continue running results in the id being passed down and the call returning me the values I expect.

    Anyone any ideas on how to fix this?

  6. #6
    Sencha Touch Premium User
    Join Date
    Oct 2010
    Location
    Portland, Maine USA
    Posts
    66
    Vote Rating
    1
    gordonjl is on a distinguished road

      0  

    Default


    ksystems, I saw in my testing that the id was in the request that was sent to the
    Code:
     Ex.data.RestProxy.buildUrl()
    call (the request has the operation in it), it's just ignored.

  7. #7
    Sencha User
    Join Date
    Jun 2010
    Posts
    18
    Vote Rating
    0
    ksystems is on a distinguished road

      0  

    Default


    Quote Originally Posted by gordonjl View Post
    ksystems, I saw in my testing that the id was in the request that was sent to the
    Code:
     Ex.data.RestProxy.buildUrl()
    call (the request has the operation in it), it's just ignored.
    You're correct, of course. It just seems to never use the operation.id either in buildUrl or before that to apply it to operation.params before calling buildRequest(operation) where it uses operation.params to set the params property of the request.

  8. #8
    Touch Premium Member JELaVallee's Avatar
    Join Date
    Jul 2008
    Posts
    23
    Vote Rating
    0
    JELaVallee is on a distinguished road

      0  

    Exclamation Ext.data.RestProxy Implementation Temporary Fix

    Ext.data.RestProxy Implementation Temporary Fix


    From digging around in the source code for the Ext.data.RestProxy and the Ext.data.Model.load method, I don't see anywhere that the id is actually appended to the request.url in the operation.

    I don't even see any references to the appendId parameter in the RestProxy config. It would appear that the docs are describing functionality that hasn't been implemented?

    I've created my own CustomRestProxy extension that has an overridden buildUrl method which does the ID post-pending on the URL before calling the superclass buildUrl (ServerProxy's I believe).

    Here's mine:

    Code:
    Ext.ux.CustomRestProxy = Ext.extend(Ext.data.RestProxy, {
        /**
         * Generates a url based on a given Ext.data.Request object. By default, ServerProxy's buildUrl will
         * add the cache-buster param to the end of the url. Subclasses may need to perform additional modifications
         * to the url.
         * @param {Ext.data.Request} request The request object
         * @return {String} The url
         */
        buildUrl: function(request) {
            var url = request.url || this.url;
    
            if (!url) {
                throw "You are using a ServerProxy but have not supplied it with a url. ";
            }
    
            // EXTENSION - BEGIN
            // Append the node.id to the url if it exists and is not "root"
            if( this.appendId ){
                var nodeId = request.operation.node.id
    
                if( nodeId !== "root" ){
                    request.url = url + "/" + nodeId;
                }
            }
    
            // Call the super implementation for doing things like cache appending, etc.
    
            return Ext.ux.CustomRestProxy.superclass.buildUrl.call(this, request);
            // EXTENSION - END
        }
    });
    Note that I'm honoring the appendId config parameter/property but unlike the documentation, I'm not differentiating between whether this is a Model.load call or a Store.load call... Mainly because I don't need such a differentiation in my app. Mileage may vary...

    Anyone from Sencha Dev Team care to comment?

    Thanks,
    Etienne
    Last edited by JELaVallee; 1 Mar 2011 at 10:43 AM. Reason: fix typo
    Jean-Etienne LaVallee
    Senior Consultant - Owner
    iflection! Innovation Group, Inc.

  9. #9
    Sencha User
    Join Date
    Jun 2010
    Posts
    18
    Vote Rating
    0
    ksystems is on a distinguished road

      0  

    Default


    Yes, clearly this seems like it's been overlooked and looking at preview 2 of Ext 4 it seems it will suffer from the same problem.

    My question is how could this get past all those thousands of unit tests that were talked about at the conference as I can't see how this has never worked? I am assuming without hacking it ourselves there is no way to get this work?

    I'll look at the solutions proposed above thanks.

  10. #10
    Touch Premium Member JELaVallee's Avatar
    Join Date
    Jul 2008
    Posts
    23
    Vote Rating
    0
    JELaVallee is on a distinguished road

      0  

    Default


    I'm baffled too... really not the sort of thing I've come to expect from the Ext/Sencha crew after years of working with ExtJS. C'est la vie...

    My solution above is not comprehensive, as I note. But it does get the job done. Since I don't use the Ext.data.Model structures directly for doing REST calls, I'm not that concerned, but it would only require a few minor overrides to the Model methods to get them in-line with using the id field properly.

    word,
    Etienne
    Jean-Etienne LaVallee
    Senior Consultant - Owner
    iflection! Innovation Group, Inc.

Similar Threads

  1. Loading Multiple Models by Association using Store
    By babar.sajjad in forum Sencha Touch 1.x: Discussion
    Replies: 3
    Last Post: 31 Mar 2011, 6:56 AM
  2. Load Multiple Models using Store.
    By irfaniqbal in forum Sencha Touch 1.x: Discussion
    Replies: 0
    Last Post: 26 Jan 2011, 6:30 AM
  3. RpcProxy how to load multiple models ...super torture bug ?
    By gauravpd in forum Ext GWT: Discussion
    Replies: 2
    Last Post: 28 Apr 2010, 3:20 AM
  4. Sending additional load parameters with headerclick
    By magunes117 in forum Ext 2.x: Help & Discussion
    Replies: 0
    Last Post: 8 Feb 2008, 4:34 AM

Thread Participants: 10

Tags for this Thread