React.js + Node.js = Isomorphic JavaScript
Reduce coupling, Increasing cohesion
What's wrong with Html+JavaScript+CSS ?
Reduce coupling, Increasing cohesion ?
HTML
HTML
HTML
CSS
JavaScript
CSS
JavaScript
CSS
JavaScript
Is this correct?
Long Time ago..
Template
(but the occasional loop or conditional is ok...)
a.k.a MVVM
the React.js team at Facebook
Component
Component
Component
Our Goal..
So does Taobao did!
/*JavaScript*/
App.ConfirmButtonComponent = Ember.Component.extend({
actions: {
showConfirmation: function() {
this.toggleProperty('isShowingConfirmation');
},
confirm: function() {
this.toggleProperty('isShowingConfirmation');
this.sendAction('action', this.get('param'));
}
}
});
<!--html-->
{{#if isShowingConfirmation}}
<button {{action "confirm"}}>Click again to confirm</button>
{{else}}
<button {{action "showConfirmation"}}>{{title}}</button>
{{/if}}
HTML and JavaScript are not cohesive
Also the biggest problem is that the components are not composable.
<!--html-->
{{#if isShowingConfirmation}}
<button {{action "confirm"}}>Click again to confirm</button>
{{else}}
<button {{action "showConfirmation"}}>{{title}}</button>
{{/if}}
How could you compose those ugly HTML templates
with the correct variable scope?
{{#each todo in todos}}
<p>{{todo.title}} {{confirm-button title="Delete" action="deleteTodo" param=todo}}</p>
{{/each}}
(YUI, Backbone, Taobao Kissy have the same problem, too.)
angular.module('docsTransclusionExample', [])
.controller('Controller', ['$scope', function($scope) {
$scope.name = 'Tobias';
}])
.directive('myDialog', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
templateUrl: 'my-dialog.html',
link: function (scope, element) {
scope.name = 'Jeff';
}
};
});
HTML and JavaScript are not cohesive
But the components are composable!
Angular.js build its own scope system in each component
<html>
<body>
<parent>
<child><child>
</parent>
</body>
</html>
angular.directive("child",function(){
restrict:'E',
templateUrl:'child.html',
scope:true
});
angular.directive("child",function(){
restrict:'E',
templateUrl:'child.html',
//scope參數不要設定
});
angular.directive("child",function(){
restrict:'E',
templateUrl:'child.html',
scope:{}
});
Isolated scope
Inherited scope
Shared scope
Angular.JS is great, but HTML and JavaScript are still not cohesive...
Facebook and Instagram have applied this practice
in production since 2011
Yahoo,Firefox, Airbnb ...using React.js
NPM
NPM
NPM
Which means the componets are reusable and composable!
Leverage form Node.js Standard without building its own scope system.
It can require css too!
Testable functions
Easy to cowork with Designers
All in one file!!
(Even CSS!)
JSP
JavaScript + CSS
Server
Client
Display logic here
Not so flexable..
There is always some logic in client side..
Java or Ruby ...(only API)
Angular.js or Ember.js + CSS
Server
Client
Display logic here
Not SEO friendly..
and Client side is too heavy
React.js Componets (JavaScript)
React.js Componets (JavaScript)
Server
Client
Display logic here
One display logic, run everywhere!
Isomorphic JavaScript
React.js build Virtual DOM to reder DOM on server side!
Isomorphic JavaScript
Yahoo Uwant Wall 2013
1.Need more F2E engineers
2.Need Back-end API support
3.Carefully separate business logic and display logic
1. SEO friendly
2. Componet Reusable
3. Componet Composable
4. Using NPM standard, easy to deal with dependencies
5. HTML+CSS+JS are cohesive
6. View and display logic are coupled
7. Easy for testing
8. More robust front end
Using diff to update real dom
Extremely fast
the React.js team at Facebook
using JSX to create elements
Easily to spot the bug.
Your app might already talks to other services over HTTP
https://github.com/facebook/react/tree/master/examples/server-rendering