ded / bonzo

library agnostic, extensible DOM utility
Other
1.32k stars 137 forks source link

Shouldn't bonzo.create() return the resultant DOMElement instead of an array containing it? #95

Closed aawwawa closed 11 years ago

aawwawa commented 12 years ago

Maybe I'm just not getting it, but it's thrown me for a loop on several occasions that to obtain a native DOMElement from bonzo.create() one needs to take its [0].

It's extremely counterintuitive to me that its return-value is sort of neither here nor there -- not quite as sexy as a bonzo object, and yet it's also not as straightforward as a native DOMElement, either: just a clumsy worst-of-both-worlds situation somewhere in between.

I always instinctively assume that if I just do bonzo.create('<alksdj>'), then I'll get a native element because if I want to get a bonzo object, I still need to wrap it in bonzo() anyway, like so: bonzo(bonzo.create('<foo>')).

It's doubly bad, because after I finally figure out that #94 is what's been breaking my code, I move on from the create() statement, naively assuming that the remaining bugs must lie elsewhere.

I should note that I'm pretty new to ender, so it could very possibly be just me. Another possibility is that this is super bikesheddy, and should accordingly be totally ignored by any serious developers reading it as a silly n00b-rant -- take it with a grain of salt.

Cheers!

connor commented 11 years ago

bonzo.create() returns an array because you can .create() multiple elements.

example:

var x = bonzo.create("<h3>hello, world</h3><p>some content</p>")
// x[0] is the h3
// x[1] is the p

This spurred me to start a branch that returns the element, if the resulting array is just one-item long.

so, basically changing this to:

return els.length === 1 ? els[0] : els

i'm not sure what @rvagg or @ded think of this though, seeing others have already adopted the array-like approach. i havent put this up for a PR yet because i'd like to hear what they think, and i haven't fixed the tests up to work with this approach yet.

what do you all think?

rvagg commented 11 years ago

I prefer the consistency of returning the array although I think my preference is to not create multiple elements at the same time -- if I'm doing that then they are nested under a single parent anyway. But, create() is used a lot internally, for the manipulation methods, consider replaceWith('<h3>hello, world</h3><p>some content</p>') for example. There's also the surprise-factor which should be minimised in a public API IMO, return a single in one instance and an array in another? So, in general, I don't feel too strongly about any of this but overall I'm -1.

ded commented 11 years ago

Others have wanted this too, but like @rvagg said, it's the consistency that mostly matters here. Plus, if that changed, a lot of peoples code would break. a simple case such as this:

bonzo.create('<div/>').addClass('eyo').appendTo(document.body)

the end result will have to be to add a new kind of function. thanks for understanding :)