1. #1
    Touch Premium Member
    Join Date
    Jan 2011
    Posts
    16
    Vote Rating
    1
    Answers
    2
    adamatvips is on a distinguished road

      0  

    Default Answered: Convert Boolean

    Answered: Convert Boolean


    Sencha doesn't seem to use the same logic for converting to boolean as JS which leads to some confusion.

    Ext.data.Types does the conversion with
    Code:
    if (this.useNull && (v === undefined || v === null || v === '')) {
        return null;}
    return v === true || v === 'true' || v == 1;
    which means things like -1 and most other truthy combinations don't return true.

    Why isn't it simply
    Code:
    if (v)
    return true;
    else
    return false;
    which would then match JS?

  2. I did forget the usenull, so would need to copy that line into my fix.

    As for -1 in JS not being true, it isn't == true but it is truthy.

    Also, the code doesn't just check if it exists otherwise v=0 would return true.
    Code:
      BOOL: {
                convert: function (v) {
                     if (this.useNull && (v === undefined || v === null || v === '')) {
                        return null;
    
                     if (v)
                        return true;
                    else
                        return false;
                },
                sortType: st.none,
                type: 'bool'
            }
    Other option which is less intuitive but same result

    Code:
      BOOL: {
                convert: function (v) {
                     if (this.useNull && (v === undefined || v === null || v === '')) {
                        return null;
    
                     return !!v;
                },
                sortType: st.none,
                type: 'bool'
            }

  3. #2
    Sencha - Services Team
    Join Date
    Mar 2007
    Location
    Foristell, MO
    Posts
    1,108
    Vote Rating
    3
    Answers
    12
    jratcliff is on a distinguished road

      0  

    Default


    your code is just checking if v exists not if it is true or false. And besides, I don't think -1 in Javascript is true.

    For example, if you set v to -1 and run this code you will get false and this is basically what the code in Ext.data.Types is doing except it will return null if the useNull property is set to true.

    Code:
    if (v == true) {
        return true;
    } else {
        return false;
    }
    Jack Ratcliff
    Sencha Inc, Green bleeding Senchan

    How to report a bug:
    http://www.sencha.com/forum/showthre...o-report-a-bug

  4. #3
    Touch Premium Member
    Join Date
    Jan 2011
    Posts
    16
    Vote Rating
    1
    Answers
    2
    adamatvips is on a distinguished road

      0  

    Default


    I did forget the usenull, so would need to copy that line into my fix.

    As for -1 in JS not being true, it isn't == true but it is truthy.

    Also, the code doesn't just check if it exists otherwise v=0 would return true.
    Code:
      BOOL: {
                convert: function (v) {
                     if (this.useNull && (v === undefined || v === null || v === '')) {
                        return null;
    
                     if (v)
                        return true;
                    else
                        return false;
                },
                sortType: st.none,
                type: 'bool'
            }
    Other option which is less intuitive but same result

    Code:
      BOOL: {
                convert: function (v) {
                     if (this.useNull && (v === undefined || v === null || v === '')) {
                        return null;
    
                     return !!v;
                },
                sortType: st.none,
                type: 'bool'
            }

  5. #4
    Sencha - Services Team
    Join Date
    Mar 2007
    Location
    Foristell, MO
    Posts
    1,108
    Vote Rating
    3
    Answers
    12
    jratcliff is on a distinguished road

      0  

    Default


    Ah yes, you're right. -1 is truthy. Perhaps you can post this in the Bugs forum if you want the core devs to consider changing the code.
    Jack Ratcliff
    Sencha Inc, Green bleeding Senchan

    How to report a bug:
    http://www.sencha.com/forum/showthre...o-report-a-bug

Thread Participants: 1