@gerardsans
Code Lab: bit.ly/angular-labs-rxjs
Slides: bit.ly/introduction-to-rxjs
Blog post: bit.ly/rxjs-angular
http://bit.ly/introduction-to-rxjs @angularjs_labs
http://bit.ly/introduction-to-rxjs @angularjs_labs
http://bit.ly/introduction-to-rxjs @angularjs_labs
for(var i=0, ii=team.length; i<ii; i+=1){
console.log(team[i].name);
}
team.forEach(function(member){
console.log(member.name);
});
var newTeam = [];
for(var i=0, ii=team.length; i<ii; i+=1){
newTeam.push(team[i]);
}
var onlyNames = team.map(function(member){
return { name: member.name };
});
var onlyOver100Commits = [];
for(var i=0, ii=team.length; i<ii; i+=1){
if (team[i].commits>100) {
onlyOver100Commits.push(team[i]);
}
}
var onlyOver100Commits = team
.filter(function(member){
return (member.commits>100);
});
http://bit.ly/introduction-to-rxjs @angularjs_labs
var total = 0; // initial value
for(var i=0, ii=team.length; i<ii; i+=1){
total = total + team[i].commits;
}
var total = team
.reduce(function(total, member){
return total + member.commits;
}, 0);
http://bit.ly/introduction-to-rxjs @angularjs_labs
team
.filter(over100Commits)
.map(projectName)
.map(toUpperCase)
.forEach(log);
function over100Commits(member){
return (member.commits>100);
}
function projectName(member){
return member.name;
}
function toUpperCase(input){
return input.toUpperCase();
}
http://bit.ly/introduction-to-rxjs @angularjs_labs
----0----1----2----3----> rx.interval(5000)
$scope.counter = 0;
rx.Observable
.interval(5000)
.take(3)
.safeApply($scope, function(x) {
$scope.counter = x;
})
.subscribe(); // shows 0, 1, 2
http://bit.ly/introduction-to-rxjs @angularjs_labs
Observable
Operations
Subscribe
http://bit.ly/introduction-to-rxjs @angularjs_labs
observable.subscribe(
function onNext(value){
console.log(value);
},
function onError(error){
console.log(error);
},
function onCompleted(){
console.log('Completed');
});
http://bit.ly/introduction-to-rxjs @angularjs_labs
var observer = rx.Observer.create(
function onNext(result){
console.log(result);
},
function onError(err){
console.log(err);
},
function onCompleted(){
console.log('Completed');
}
);
observable.subscribe(observer);
http://bit.ly/introduction-to-rxjs @angularjs_labs
var subscriber = observable.subscribe(observer);
$scope.$on("$destroy", function() {
subscriber.dispose();
});
@angularjs_labs