You found a bug! We've classified it as EXTJS-3568 . We encourage you to continue the discussion and to find an acceptable workaround while we work on a permanent fix.
  1. #1
    Sencha User
    Join Date
    Apr 2010
    Location
    Toronto
    Posts
    36
    Vote Rating
    1
    Chao is on a distinguished road

      0  

    Default Possible REST proxy bug when record's id is 0

    Possible REST proxy bug when record's id is 0


    I have a RESTful proxy set up with a Ext.data.Store
    Code:
    proxy : {
            type : 'rest', 
    	url : 'productsHandler',
    	reader : {
    	     type : 'json',
    	     root : 'products'
    	}
    }
    When I update a record in that store with id = 1, I get this URL: /productsHandler/1?_dc=..... which is perfect, when when my record's id = 0, I get this URL : /productsHandler/?_dc=... which is incorrect because the id is missing.

    I tried digging around a little bit, and found this, I think this could be the cause:
    Code:
     
    buildUrl: function(request) {
           var me        = this,
                operation = request.operation,
                records   = operation.records || [],
                record    = records[0],
                format    = me.format,
                url       = me.getUrl(request),
                id        = record ? record.getId() : operation.id;
            
            if (me.appendId && id) {
                if (!url.match(/\/$/)) {
                    url += '/';
                }
                
                url += id;
            }
            ......//more code
    As you can see, if the id = 0, that bolded "if" statement would evaluate to false. Please take a look at this.

    Thanks

  2. #2
    Sencha User mberrie's Avatar
    Join Date
    Feb 2011
    Location
    Bangkok, Thailand
    Posts
    506
    Vote Rating
    14
    mberrie will become famous soon enough mberrie will become famous soon enough

      0  

    Default


    Bug confirmed!

    REQUIRED INFORMATION



    Ext version tested:
    • Ext 4.0.2a

    Browser versions tested against:
    • Chrome 14.0.825.0 dev-m
    • FF5.0
    most likely all browsers if they follow the Javascript Spec

    Description:
    • Rest proxy does not append id to url when loading a model/record with id '0' (as int).

    Steps to reproduce the problem:

    • Create a Ext.model.Model
    • try to load instance with id '0', like MyModel.load(0);

    The result that was expected:
    • a url request to /api/users/0?_dc=..

    The result that occurs instead:
    • a url request to /api/users/?_dc=..

    Test Case:

    Code:
    Ext.require('Ext.ModelManager');
    
    Ext.define('User', {
        extend: 'Ext.data.Model',
        fields: ['first', 'last'],
        proxy: {
            type: 'rest',
            url : '/api/users'
        }
    });
    
    Ext.onReady(function() {
        Ext.ModelManager.getModel('User').load(1); // CORRECT: /api/users/1?_dc=..
        Ext.ModelManager.getModel('User').load(0); // BUG: /api/users/?_dc=..
    
        // check with Firebug/Developer Tools network tab
    });
    HELPFUL INFORMATION


    Screenshot or Video:
    • sure


    Debugging already done:
    • yes

    Possible fix:

    Code:
    Ext.require('Ext.data.proxy.Rest', function() {
        Ext.override(Ext.data.proxy.Rest, {
            buildUrl: function(request) {
                var me        = this,
                    operation = request.operation,
                    records   = operation.records || [],
                    record    = records[0],
                    format    = me.format,
                    url       = me.getUrl(request),
                    id        = record ? record.getId() : operation.id;
    
                if (me.appendId && !Ext.isEmpty(id)) { // FIX
                    if (!url.match(/\/$/)) {
                        url += '/';
                    }
    
                    url += id;
                }
    
                if (format) {
                    if (!url.match(/\.$/)) {
                        url += '.';
                    }
    
                    url += format;
                }
    
                request.url = url;
    
                return me.callParent(arguments);
            }
    
    
        });
    });
    Additional CSS used:
    • -
    Operating System:
    • Win7

  3. #3
    Sencha - Community Support Team edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    7
    edspencer is a jewel in the rough edspencer is a jewel in the rough edspencer is a jewel in the rough

      0  

    Default


    Awesome, thanks for the report. This is now in the bug tracker. We're performing a series of updates/improvements to data in the next two point releases so expect a lot of progress in that area
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

Thread Participants: 2