You found a bug! We've classified it as TOUCH-3727 . 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
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      1  

    Default Sencha Touch 2.1 marks Ajax JSON response as failed if server returns empty array

    Sencha Touch 2.1 marks Ajax JSON response as failed if server returns empty array


    In Ext.data.reader.Reader nullResultSet is defined like this:

    Code:
    // Empty ResultSet to return when response is falsy (null|undefined|empty string)        
    nullResultSet: new Ext.data.ResultSet({            
        total  : 0,            
        count  : 0,            
        records: [],            
        success: false        
    })
    
    The problem is that the Ext.data.reader.Reader#readRecords has the following code:

    Code:
     
    if (isArray && !data.length) {
        return me.nullResultSet;         
    }
    
    that causes nullResultSet to be returned not only in cases specified above, but also for empty arrays. Because nullResultSet has success flag set to false, it causes the whole response to be incorrectly marked as failed.

    BTW. I can fix it myself, but currently there's no way to contribute to Sencha Touch, right? Any plans to use GitHub or something similar for hosting Sencha Touch code?

    Here's how I fixed it (touch/src/data/reader/Reader.js)

    Code:
             if (isArray && !data.length) {
    -            return me.nullResultSet;
    +            return new Ext.data.ResultSet({
    +                total  : 0,
    +                count  : 0,
    +                records: [],
    +                success: true
    +            });
             }

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,227
    Vote Rating
    859
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Thanks for the report! I have opened a bug in our bug tracker.

  3. #3
    Sencha User
    Join Date
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      0  

    Default


    I've just noticed that this ticket is closed, but not fixed, and it says "Looks like we can't reproduce the issue or there's a problem in the test case provided". What does it mean exactly? Do you want me to provide some more info or a test case?

  4. #4
    Sencha User
    Join Date
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      0  

    Default


    To make it easier for you guys to reproduce this issue, here's a screencast: http://www.screencast.com/users/szim...0-1fc79e11833d

    The first call to Ext.getStore("Resources").load() fetches 2 records and properly displays them in the list view. The second call fetches an empty array (valid JSON) and throws exception event instead of displaying empty list. If you want I can upload code to GitHub, but it's just a single list view + model with name field and store with REST proxy and JSON reader.

    Hope that helps reproducing this issue.

    Cheers,
    Szymon

  5. #5
    Sencha User
    Join Date
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      0  

    Default


    Bump. I really do hope you'll reopen this issue after watching the screencast.

  6. #6
    Sencha User
    Join Date
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      0  

    Default


    Bump. Reposting link to screencast: http://www.screencast.com/users/szim...0-1fc79e11833d

  7. #7
    Sencha Premium Member
    Join Date
    May 2012
    Posts
    9
    Vote Rating
    1
    jensenl is on a distinguished road

      0  

    Default


    I have seen the same thing in my app. I worked around it by calling removeAll instead of setData when I get an empty array. If this is not a bug, it is annoying that they have decided to change the way empty arrays are handled. All of my web services and code were setup on the assumption that an empty array was okay.

  8. #8
    Sencha User
    Join Date
    Jan 2012
    Posts
    5
    Vote Rating
    0
    dolphin278 is on a distinguished road

      0  

    Default


    It got fixed by returning empty ResultSet, with 0 members, BUT, status is set to false, so if you have some items in your store, and server response returns [], old entries from your store are not deleted from your store, event if they are not existing on server :-(

  9. #9
    Sencha User
    Join Date
    Oct 2012
    Location
    Poland
    Posts
    70
    Vote Rating
    5
    szimek is on a distinguished road

      0  

    Default


    I fixed it by creating an `emptyResultSet` that is almost exactly the same as `nullResultSet` but has success flag set to true (a slightly more clean approach than in my first post).

    But it would be nice if someone from Sencha confirmed if it's a desired behavior (and if yes, then why) or a bug.

  10. #10
    Sencha User
    Join Date
    Jan 2012
    Posts
    5
    Vote Rating
    0
    dolphin278 is on a distinguished road

      0  

    Default


    I changed server API so it returns not only raw data but also 'success' property, that also solves the problem.