News for hackers. Stuff that matters.
Re:Removing User Interface Complexity, or Why React is Awesome by nostrademons on Tuesday May 13, 2014 @02:26PM (#7739755)
I think this post is missing something in its description of Web Components: the fundamental difference between a JS-based framework like React and a Web Components-based framework like Polymer is that the former takes JS objects as primitives and the DOM as an implementation artifact, while the latter takes the DOM as a primitive and JS as an implementation artifact. You cannot wrap your head around Web Components and give both it and JS frameworks a fair shake until you can make this mental shift in perspective fluently.

The line in the post where "You can't even do something as basic as that with Web Components.":

  var MyToolbar = require('shared-components/toolbar');
In fact has a direct analogue with HTML imports:

  <link rel="import" href="shared-components/toolbar.html">
And that's key to understanding Web Components. The idea of the standard is that you can now define your own custom HTML elements, and those elements function exactly like the DOM elements that are built into the browser. This is a key strategic point: they function exactly like the DOM elements that are built into the browser because Google/Mozilla/Opera/et al hope to build the popular ones into the browser eventually, just like we've gotten <input type=date> and <details>/<summary> based on common web usage patterns.

A number of the other code samples in the article also have direct analogues in Polymer as well. For example, the App/Toolbar example halfway down the page would be this:

  <polymer-element name="Toolbar" attributes="number">
    <template>
      <div>
        <button value="increment" on-click="{{increment}}">
        <button value="decrement" on-click="{{decrement}}">
      </div>
    </template>
    <script>
      Polymer('toolbar', {
        number: 0,
        increment: function() { this.number++; }
        decrement: function() { this.number--; }
      });
    </script>
  </polymer-element>

  <polymer-element name="App">
    <template>
      <div>
        <span>{{toolbar.number}}</span>
        <Toolbar number=0 id="toolbar"></Toolbar>
      </div>
    </template>
    <script>
      Polymer('App', {
        created: function() {
          this.toolbar = this.$.toolbar;
        }
      });
    </script>
  </polymer-element>
You can decide for yourself whether you like that or you like the Bloop example more - my point with this post is to educate, not evangelize - but the key point is that you can define your own tags and elements just like regular DOM elements, give them behavior with Javascript, make them "smart" through data-binding so you don't have to manually wire up handlers, and then compose them like you would compose a manual HTML fragment.
Re:Removing User Interface Complexity, or Why React is Awesome by jlongster on Tuesday May 13, 2014 @03:05PM (#7739979)
The problem is that it doesn't scale complexity-wise. You miss a key point with imports: A `<link>` imports something into the global scope. There's always one global scope. In my example, I was able to rename Toolbar into MyToolbar and use it in my scope. You lose so much using the DOM as primitive and JS as artifact.

And the second you start doing conditional elements (like the tabbed example), Web Components gets pretty complex. In React, it's just `if(shown) { renderItem(); }`.

If you make HTML smart enough, sure, you don't have to manually wire up anything. But then you're left with `x-if`, `x-ajax`, and so on tags. Why not just use JavaScript?

Re:Removing User Interface Complexity, or Why React is Awesome by spankalee on Tuesday May 13, 2014 @03:35PM (#7740153)
Using JavaScript is fine for things that need script, but ultimately, if I'm building HTML, I'd rather do it in HTML. Put another way: HTML is a great DSL for creating HTML.

As for scoping of imports, you do have a point there in theory, but in dealing with quite a few complex Polymer applications at this point I haven't seen that be an issue in practice. The scoping issue is well known among the people working on Web Components, and AFIAK there are plans to address it.

I'm not sure why you think conditionals are complex in templates. Your example might look like this in a Polymer template:

  <template if="{{ shown }}>
    <my-item></my-item>
  </template>
Yes, there's some token overhead, but in the context of a larger template it pays off to have the entire HTML specified in one place, and in a way that mirrors it's output structure, not spread out over often hard to follow imperative code.

However, as you point out, Web Components doesn't prescribe how to manipulate the DOM, so if you still prefer to do it in script, go ahead!

The point is to produce an element where the browser, developer tools, and your users don't have to care what choice you made.

Re:Removing User Interface Complexity, or Why React is Awesome by peterhunt on Tuesday May 13, 2014 @04:07PM (#7740374)
HTML is a great DSL for creating static HTML.

When you start to bolt on features for dynamism in HTML you end up with Angular. The reason that it has all of those complex features is because it needs them to build real world apps. I'm sure once people have built more stuff with Polymer you'll start to see similar "feature creep".

Re:Removing User Interface Complexity, or Why React is Awesome by spankalee on Tuesday May 13, 2014 @04:26PM (#7740487)
I've worked with a number of very complex Polymer applications, probably the most complex Polymer applications in existence right now, and the template features have been sufficient so far, and there's always the fallback to code if necessary.

I can think of very few common feature requests (mostly on Shadow DOM, actually), and they don't being to approach the complexity of Angular, which concerns itself with so much more than templating. Angular's complexity is not simply an unavoidable consequence of being used for real world apps - it's just complex.

Re:Removing User Interface Complexity, or Why React is Awesome by nostrademons on Tuesday May 13, 2014 @06:15PM (#7740990)
In practice you'd be namespacing your tags, so it'd be things like <polymer-ajax>, <google-slider>, <facebook-like-button>, etc. At scale you want this, as reading the code becomes quite confusing when you rename each component to your own custom name. It does make typing them out quite a pain (as anyone who's worked with YUI or Google Closure can attest), but that's the price you pay for large-scale development.
Re:Removing User Interface Complexity, or Why React is Awesome by munificent on Wednesday May 14, 2014 @01:26AM (#7742341)
We reinvent C-style prefixes-as-fake-namespaces in HTML tag names when we have a perfectly good Turing-complete language with actual scoping rules sitting right here?
Re:Removing User Interface Complexity, or Why React is Awesome by nostrademons on Wednesday May 14, 2014 @01:40AM (#7742379)
I don't mean to evangelize Polymer here - I don't actually have a horse in this race, having recently left Google and being still undecided about which technology my startup will be based upon. I think that the prefixes-as-fake-namespaces approach sucks, but of all the things you can get wrong with your web framework, it sucks a lot less than many other things framework authors do. And it enables the abstraction of "Treat web components like any other HTML tag, except you get to define their object model", which is a really powerful concept, particularly when you consider the possibilities of building the most popular of them into the browser. The problem with all existing JS libraries so far is that they become obsolete as browser technology advances; Web Components is the first one where the components may become the browser technology advances.

To complete your C analogy - yes, C sucks a lot, and it's woefully incomplete in areas like namespacing and packaging. But people still use C, because if you want to do something native it's basically the only choice, and you know that it will interoperate with basically every other language out there. And those people have managed to build some pretty impressive things with C, warts and all, even though a lot of its design choices "will never work" when programming at scale.

Re:Removing User Interface Complexity, or Why React is Awesome by spankalee on Wednesday May 14, 2014 @02:39AM (#7742503)
There's a really good chance that we'll get scoping for element registration at some point, given that the shadow trees are natural scoping boundaries.
Re:Removing User Interface Complexity, or Why React is Awesome by natural219 on Tuesday May 13, 2014 @04:15PM (#7740424)
In Ember, for instance, it's perfectly easy for a "computed property" on a View object to return the value of an attribute on a DOM node. In this model, the DOM is the "canonical" representation of state (also fulfilling HATEOAS if "real REST" is your thing). This is a perfectly fine way of doing things, and is possible in Ember or React or any of the above frameworks as far as I'm aware. How the binding between javascript and DOM is registered is entirely up to the library developer.

In my ideal world, I want the performance of the Virtual DOM / rAF batching described in the blog post, I want web components as the underlying DOM / markup that represents my views, and I want to wrap them with Ember-like computed properties and routing structures. But hey, while we're asking for everything, I'd also like universal flexbox implementation, Clojure-to-asm.js compilers, ES6 generators to finally come out...

Re:Removing User Interface Complexity, or Why React is Awesome by ebryn on Tuesday May 13, 2014 @11:28PM (#7742032)
I think you'll be happy with the stuff I've been working on. Should have something to show off in the coming weeks :)
Re:Removing User Interface Complexity, or Why React is Awesome by Touche on Wednesday May 14, 2014 @09:02AM (#7743457)
> The line in the post where "You can't even do something as basic as that with Web Components."

Actually that's factually inaccurate. You certainly can do that. registerElement returns a Constructor function, so you can export that in your module if you want to be able to require it elsewhere.

Re:Removing User Interface Complexity, or Why React is Awesome by skybrian on Tuesday May 13, 2014 @02:39PM (#7739834)
It's also not either-or. If you have components written in Polymer that you want to reuse, in principle, a React-like framework could render them. (I don't know how well this works today.)
Re:Removing User Interface Complexity, or Why React is Awesome by spankalee on Tuesday May 13, 2014 @03:26PM (#7740105)
Thank you very much for making the point I was about to.

I was very confused about the Web Components criticism, it seemed like the author didn't understand Web Components at all. My first thought was, "Why would you want to import your element into JavaScript, unless you're going to be doings something scripty with it?"

What you really want to do in a custom elements world, is embrace HTML, as opposed to JS, as the place to build your DOM. Instead of:

    Bloop.renderComponent(Box(), document.body);
do:

    <bloop-box></bloop-box>
Then I don't get this paragraph at all:

  > Aren't you tired of having to query the DOM tree and manually manage the structure do 
  > create UIs? Web Components doesn't solve this at all, it just tries to encapsulate the
  > work. The problem is that building apps is building components, so you inevitably are
  > forced back into the manual DOM management to create your app-specific components (like 
  > how you constantly have to create directives in Angular). You also need to jump into 
  > JavaScript to configure and wire up any Web Components you used. It's a very messy 
  > abstraction, and fools you into desiring a pure HTML-based declarative way to write 
  > apps, which like wanting steak but eating liver.
What?

Of course the point of Web Components is to encapsulate the implementation DOM and the work of manipulating that DOM, and not prescribe how it's done, because you can do it in many ways: manually use DOM APIs, use templating like in Polymer, or even use React if you want.

The author then mixes criticisms of Angular with Web Components. Angular doesn't produce Web Components, has far too many complex concepts IMO (like the $scope.$apply example), and so far has rejected a Web Components centric approach (see their 2.0 design docs). You have to create many directives in Angular because it doesn't support standard 2-way data binding to properties out of the box: without directives you can only bind to attributes. Polymer, for instance, doesn't have those problems, and very complex widgets are built with templates.

And the complaint about needing JavaScript to configure Web Components seems to be completely without merit. Web Components imported with HTML imports are fully capable of registering themselves, so the import is all you need. Yes there's some JS under the hood, but you don't see it, and all of "Bloop" is JS anyway, so I'm really lost on this point.

React may be great, but the future is exactly in encapsulating away the work of building custom elements so that an application can be built of elements sourced from different vendors using whatever tools they want to implement them. I would have no problem using custom elements built with React, but at this point I am not interested in components that force me to use a particular framework. Trying to proclaim that all apps should be built with this or that awesome framework, is a quickly aging point of view.

Re:Removing User Interface Complexity, or Why React is Awesome by benatkin on Tuesday May 13, 2014 @04:59PM (#7740670)
According to the Web Component spec a custom element must contain a dash. Also the convention is to use lowercase. http://w3c.github.io/webcomponents/spec/custom/#concepts
Re:Removing User Interface Complexity, or Why React is Awesome by nostrademons on Tuesday May 13, 2014 @06:11PM (#7740976)
I know, but I wanted to make the correspondence between the examples as clear as possible, including using the same names. In practice it'd be something like <my-toolbar> and <my-app>. (Well, in practice it'd be something like <polymer-toolbar> and <companyfoo-app>, but hopefully people get the point.)