-
30 Nov 2011 2:39 PM #1
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
which means things like -1 and most other truthy combinations don't return true.Code:if (this.useNull && (v === undefined || v === null || v === '')) { return null;} return v === true || v === 'true' || v == 1;
Why isn't it simply
which would then match JS?Code:if (v)
return true;elsereturn false;
-
Best Answer Posted by adamatvips
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.
Other option which is less intuitive but same resultCode: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' }
Code:BOOL: { convert: function (v) { if (this.useNull && (v === undefined || v === null || v === '')) { return null; return !!v; }, sortType: st.none, type: 'bool' }
-
30 Nov 2011 3:17 PM #2Sencha - Services Team
- Join Date
- Mar 2007
- Location
- Foristell, MO
- Posts
- 1,100
- Vote Rating
- 3
- Answers
- 12
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
-
30 Nov 2011 3:33 PM #3
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.
Other option which is less intuitive but same resultCode: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' }
Code:BOOL: { convert: function (v) { if (this.useNull && (v === undefined || v === null || v === '')) { return null; return !!v; }, sortType: st.none, type: 'bool' }
-
5 Dec 2011 7:11 AM #4Sencha - Services Team
- Join Date
- Mar 2007
- Location
- Foristell, MO
- Posts
- 1,100
- Vote Rating
- 3
- Answers
- 12
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


Reply With Quote