Results 1 to 4 of 4

Thread: Convert Boolean

  1. #1
    Touch Premium Member
    Join Date
    Jan 2011
    Posts
    17
    Answers
    2
    Vote Rating
    2
      0  

    Default 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,113
    Answers
    13
    Vote Rating
    5
      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
    17
    Answers
    2
    Vote Rating
    2
      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,113
    Answers
    13
    Vote Rating
    5
      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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •