Two-way Bindings
Angular | Ember | Knockout
@gundersenMarius
My name is
Marius Gundersen
I work as a frontend consultant with
You should follow me on twitter
@gundersenMarius
Two-way Bindings
Binding HTML and JS
<input
bind="message"
value="" />
var email = {
message: ""
};
This is not a tutorial or an introduction
I'm going to skip some details
HTML
<span data-bind="text: message"></span>
Preview
JavaScript
var message = ko.observable();
message("Hello, JSConf");
Handlebars
<span>{{ message }}</span>
Preview
JavaScript
var email = Ember.Object.create({
message: ""
});
email.set('message', "Hello, JSConf");
Html
<span>{{ message }}</span>
Preview
JavaScript
$scope.message;
$scope.message = "Hello, JSConf";
$scope.message = "Hello, JSConf";
$scope.$apply();
$scope.$apply
$scope.message = "Hello, JSConf";
$scope.$apply();
for each property in $scope
has it changed?
update the DOM
save the new value
Dirty checking
Observable Properties
✘
✔
✔
Was Ember.js at rendering the list?
Computed Properties
Celcius to Farenheit
F = C*9/5 + 32;
$scope.C = 0;
$scope.F = function(){
return $scope.C*9/5 + 32;
}
Dirty checking
F is recomputed when anything in the $scope changes!
Ember.Object.create({
C: 0,
F: function(){
return this.get('C')*9/5 + 32;
}
});
Ember.Object.create({
C: 0,
F: function(){
return this.get('C')*9/5 + 32;
}.property('C')
});
Ember.Object.create({
if_A_then_B_else_C: function(){
if(this.get('A')){
return this.get('B');
}else{
return this.get('C');
}
}.property('A', 'B', 'C')
});
Even if A is true, the property will recompute when C changes value...
var C = ko.observable(0);
var F = ko.computed(function(){
return C()*9/5 + 32;
});
var A = ko.observable(true);
var B = ko.observable(0);
var C = ko.observable(0);
var if_A_then_B_else_C = ko.computed(function(){
if(A()){
return B();
}else{
return C();
}
});
Computed Properties
✘
✔
✔
Computed Properties
✘
✔
✔
Dynamic Dependencies
✘
✘
✔
Summary
Summary
- Syntax
- Dirty Checking vs Observable Properties
Summary
- Syntax
- Dirty Checking vs Observable Properties
Summary
- Syntax
- Dirty Checking vs Observable Properties
- Synchronous vs Asynchronous
Summary
- Syntax
- Dirty Checking vs Observable Properties
- Synchronous vs Asynchronous
Summary
- Syntax
- Dirty Checking vs Observable Properties
- Synchronous vs Asynchronous
- Computed Properties
Summary
- Syntax
- Dirty Checking vs Observable Properties
- Synchronous vs Asynchronous
- Computed Properties
- Performance
Which one is fastest/slowest?
Ember is slowest when rendering lists
Angular is slowest when the model is complex
Knockout is slowest when pushing many items
Thank You
@gundersenMarius