-
-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSX second null argument compatibility fix #772
Conversation
args[i - 1] = arguments[i]; | ||
} | ||
// JSX compatibility with 'null' {pairs} argument | ||
var args = Array.prototype.slice.call(arguments, (pairs === null ? 2 : 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: just set i
to pairs === null ? 2 : 1
. Otherwise, you're creating a perf regression.
(Chrome and Firefox both don't usually like it when you pass the arguments
object as an parameter. This is one case.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I'll fix it.
On Sat, Aug 22, 2015 at 9:53 AM Isiah Meadows notifications@github.com
wrote:
In mithril.js
#772 (comment):@@ -49,9 +49,9 @@ var m = (function app(window, undefined) {
*
*/
function m(tag, pairs) {
for (var args = [], i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
}
// JSX compatibility with 'null' {pairs} argument
var args = Array.prototype.slice.call(arguments, (pairs === null ? 2 : 1));
Nit: just set i to pairs === null ? 2 : 1. Otherwise, you're creating a
perf regression.(Chrome and Firefox both don't usually like it when you pass the arguments
object as an parameter. This is one case.)—
Reply to this email directly or view it on GitHub
https://github.com/lhorie/mithril.js/pull/772/files#r37693412.
Babel and other JSX compilers pass 'null' value as second argument to m() function if there is no attributes in element specified. Added check for 'null' to support JSX compilers transparently. NOTE that passing children elements as arguments instead of Array is already supported.
@lhorie is this useful? |
Actually supporting I think that lot of Mithril supporters hate JSX. But transparently supporting it makes Mithril ultra-popular. Shure, transparently supporting of JSX and some onfocus/onblur preserving parts for editboxes makes Mithril obsolete React. |
Not everyone in the Mithril community hates JSX, or we wouldn't have MSX. On Fri, Aug 28, 2015, 09:41 Ivan Borisenko notifications@github.com wrote:
|
Guys from the MSX team said that they will deprecate MSX in favor of Babel,
|
I was talking about part of the community, not the MSX compiler itself. I On Fri, Aug 28, 2015, 10:44 Ivan Borisenko notifications@github.com wrote:
|
But he's not mentioned that secondary |
} | ||
|
||
if (isObject(tag)) return parameterize(tag, args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: shouldn't pairs
be sent as-is as well to parametrize
, in the form of the first part of args
? Something like this?
for (var args = [], i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
if (isObject(tag)) return parameterize(tag, args);
if (pairs === null) args.shift();
It's a breaking change otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should review all cases:
/// Base Mithril cases:
// arguments.length === 1, pairs is undefined
m('div')
// arguments.length === 2+, pairs is Object, next arguments is undefined or Array (the single third argument) or one or more functions
m('div', {id: '1'}, ...)
// arguments.length === 2, pairs is Array
m('div', [m('div')])
// arguments.length === 2+, pairs is function, as next arguments should too
m('div', m('div'))
// arguments.length === 2+, pairs is String
m('div', 'Hello')
JSX emitter always emits function call with 2 or 3 arguments. In case of absent pairs
, it emits null
instead.
So for JSX, cases are following:
m('div', {})
m('div', null, m('div'))
m('div', {}, m('div'))
So, if second argument is null
, Mithril just could consider that second argument is not second, but third one.
I'll fix the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This solution would suffice, right?
function m(tag, pairs) {
var i = 1;
// Fix JSX style passing
if (arguments.length > 2 && pairs === null) {
i = 2;
pairs = arguments[2];
}
for (var args = []; i < arguments.length; ++i) {
args.push(arguments[i]);
}
if (isObject(tag)) return parameterize(tag, args);
// ...
}
@avesus Please add unit tests for this. Here's what I see is needed with this (at least):
|
@IMPinball not shure why consider Tests are very important part, I hope to write them on the next week. Have a nice weekend, thanks for the good peer review! |
var Foo = {
view: function (ctrl, opts) {
opts = opts || {}
var children = [].slice.call(arguments, 2).map(function (value, i) {
return m('div', 'Index: ' + i, 'Body: ', value)
});
return m('div', [opts.header || "A list"].concat(children))
},
};
// This will weirdly throw an error in the view itself.
m(Foo, null, [m('span', 1)]);
`` |
@IMPinball sorry for my long wait. After dive deep into Mithril's source code and meeting with s-n-a-b-b-d-o-m (with dashes to not promote it here like an ad) I wonder about alternatives. But my aaannnsweeeer could be very slow henceforth. Feel free to bring this task to completion. I'l write when (if) come back! |
@avesus You mind redoing this against |
Closing due to inactivity. If you feel this should still be done, please feel free to file a new issue and/or pull request. 😄 |
Babel and other JSX compilers pass 'null' value as second argument
to m() function if there is no attributes in element specified.
Added check for 'null' to support JSX compilers transparently.
NOTE that passing children elements as arguments is already supported.