r/javascript • • Mar 12 '15

Data binding code in 9 JavaScript frameworks

http://engineering.paiza.io/entry/2015/03/12/145216
51 Upvotes

25 comments sorted by

12

u/hokkos Mar 12 '15

In the react example you should NOT set the state property directly (this.state.firstName = "bad code") but simply do a setState in 1 call, also there is no point in putting something computed in the state :

this.state.firstName = this.setState({
          firstName: this.refs.firstName.getDOMNode().value,
          lastName: this.refs.firstName.getDOMNode().value});

And in the render :

Full name: {this.state.firstName + " " + this.state.lastName}

0

u/yoshiokatsuneo Mar 12 '15

Thanks ! I just replace from the bad lines, where this.state is directly set, to calling setState().

3

u/jetrii Mar 12 '15

Why does the AngularJS 2.0 example include both an HTML template and a duplicate, inlined template?

0

u/yoshiokatsuneo Mar 12 '15

I just removed the duplicated part. Thanks !

10

u/[deleted] Mar 12 '15 edited Mar 12 '15

Simplified version of the React example which doesn't store derived state and doesn't require manually extracting data per-field:

http://jsfiddle.net/k5d6jhhb/3/

8

u/randfur Mar 12 '15

They should add Polymer and make it a round 10.

6

u/quitrk Mar 12 '15

Ember.JS introduced "{{}}" syntax

It's a Handlebars specific syntax. Ember is just making use of it.

2

u/TMiguelT Mar 12 '15

I think he means in his particular review, Ember was the first framework he mentioned that had mustache bindings, so they were 'introduced' by Ember.

1

u/quitrk Mar 12 '15

Oh, you might be right.

4

u/geekrelief Mar 12 '15

2

u/[deleted] Mar 13 '15

...and an MSX-ified version: http://jsfiddle.net/3ows37zc/6/ :)

2

u/[deleted] Mar 12 '15 edited Mar 13 '15

1

u/I_Pork_Saucy_Ladies Mar 12 '15

Wow, hadn't even heard of this but it does have quite a lot of stargazers on github. Any opinions on this thing? :)

2

u/[deleted] Mar 12 '15

I'm definitely going to look into Aurelia now. It looks so clean and simple compared to the rest.

3

u/morphemass Mar 12 '15

I tend to only write javascript when I have too (I have a basic grasp of AngularJS) so thats an interesting comparison. What ARE they thinking with angular 2.0 though? I want a framework that makes my life easier, not one that exponentionally increases the amount of code I have to write to perform simple basic tasks!

Aurelia.JS looks nice though.

2

u/Capaj Mar 12 '15

They wanted to get rid of the digest loop. I can't blame them. Every react fanboy is trashing angular because of it. I think you could very easily create an angular 2.0 directive with the same capabilities as ng-model. I think angular team just did not get to it yet.

2

u/vinnl Mar 12 '15

Yes, they stated at ng-conf that that will be possible, even though it's doing something simple behind the scenes.

2

u/kenavr Mar 12 '15 edited Mar 12 '15

Don't forget Angular 2 is still in alpha, if even the for loop is not fixed everything can change. But if I am not mistaken this example could be significantly less code. Similar to react.

The changed function doesn't seem to have any purpose it is not connected to any event or variable. I guess this was added for debugging. You could also combine the two "changed" functions and add an additional attribute to define which name should be changed.

app.html

First Name: <input type=text [value]="firstName" #first (keyup)="nameChanged($event, first, 'firstName')"><br/>
Last Name:  <input type=text [value]="lastName"  #last  (keyup)="nameChanged($event, last, 'lastName')"><br/>
Full Name:  {{fullName}}

app.js

 import {Component, Template, bootstrap} from 'angular2/angular2';

 // Annotation section
 @Component({
   selector: 'my-app'
 })
 @ Template({
   url: 'app.html'
 })
 // Component controller
 class MyAppComponent {
   constructor() {
     this.firstName = 'Taro';
     this.lastName = 'Yamada';
     this.fullName = this.firstName + " " + this.lastName;
   }
   nameChanged($event, object, type) {
     this[type] = object.value;
     this.fullName = this.firstName + " " + this.lastName;
   }
 }
 bootstrap(MyAppComponent);

Now if you base your judgement only on lines of code it is way better than before, but this shouldn't be a criteria anyway. As mentioned by others the digest cycle made angular 1.x in a lot of cases pretty slow and this was addressed by removing the "forced" bidirectional binding and overhauling the entire change detection. I like the new way and if the presented performance improvements are correct it was definitely the correct decision.

1

u/vinnl Mar 12 '15

I'm pretty sure that's not how actual code using Angular 2 is going to do it.

1

u/SemiNormal Mar 12 '15

Aurelia.JS makes me cringe looking at their use of periods in html tags.

0

u/[deleted] Mar 12 '15

[deleted]

2

u/kenavr Mar 12 '15

I don't know where you get this information from, but I think that's not true. The new structure comes from ES6 and typescript, people should at least get familiar with ES6.

The changes to the change detection are in a lot of ways an improvement to angular 1, sure you need to write a little bit more code, but the developer has a lot more control and getting rid of the unpredictable digest cycles made angular significantly faster.

1

u/m1sta Mar 12 '15

Would be more interesting if it included nested lists.

1

u/jrm2k6 Mar 12 '15

With react 0.13 getDOMNode is removed, so the react example is probably not going to work.

1

u/fuzzylollipop Mar 12 '15

You are missing ExtJS!

1

u/Casual_0bserver Mar 12 '15

I'm glad you included Ractive. I have been using it for 6 months now and really enjoy it.