Hello everyone,
has there been a recent breaking change i missed about $each or this ?
One user reported me today a bug i find strange :
I am using the usual blaze construct with #each
I have an onclick event handler on the checkbox class
The problem is that in the eventHandler, this is not a String Primitive, but a String Object (Wrapper Object), with typeof this = 'object'
I have doublechecked, the arrayOfStrings which is returned by a template helper contains String primitives and not String object.
This code has been working correctly for more than a year, so i guess it has been broken by a recent blaze change.
I dont want to be rude, but how exactly that minimal repo should help someone?
You dont show us in any way what is not working in your event handler.
If we see how you are trying to use event or template instance than we can comment.
Maybe you are doing something wrong way there.
I don’t see what your code has to do with the issue i’m having.
I’m just saying that in the past, this in event handler when used with string elements generated with #each was of type String, but is now of type Object.
This change broke my code in production, which has been working fine for the 9 last months.
I was askin if this was a known breaking change or a regression. I would bet for the latter since i don’t know why this in the event handler would be of type string object and not string primitive.
If the argument to #with is a string or other non-object value, it may be promoted to a JavaScript wrapper object (also known as a boxed value) when passed to helpers, because JavaScript traditionally only allows an object for this. Use String(this) to get an unboxed string value or Number(this) to get an unboxed number value.
You seems to be right Jorgeer. I didn’t know you could not use a primitive value as this.
What i’m now wondering is why this has been working fine in the past.
Thank you for your help.
I could be a side effect of the new {{#each person in persons}} syntax shipped with 1.2, but I don’t see #each documentation mentioning String(this), so either way, there was really a discrepancy you found, if it worked before
The problem is that if indeed this as primitive is not legal (and that seems pretty logical when you think about it, this is always referring to an Object), there is not a chance the old way is coming back.
I am currently auditing the broken module in my app, and i’m contemplating with horror that i have used this as a string primitive numerous times in this only module… i’m wondering how many loc are broken in my app because of this !
I’m starting to believe that those that advise to stay away from this have a pretty good point indeed !
Garrilla, that what i first wrote, but then i thought it was better to use String(this), because this.valueOf() or this.toString() will throw if this is undefined (i don’t know if this can happen…).