Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <script src="https://npmcdn.com/angular@1.5.5/angular.js"></script>
  <script src="https://npmcdn.com/rx@4.1.0/dist/rx.all.min.js"></script>
  <script src="https://npmcdn.com/rx@4.1.0/dist/rx.all.min.js"></script>
</head>
<body ng-app="app">
  <div>
    <h4>Search GitHub users</h4>
    <autocomplete></autocomplete>
  </div>
</body>
</html>
 
angular.module('app', ['rx'])
.directive('autocomplete', function () {
  return {
    scope: {},
    template: '<input ng-model="val" ng-change="update(val)" />' +
      '<ul class="suggestions">' +
        '<li ng-repeat="suggestion in suggestions">' +
          '<a href="https://github.com/{{ suggestion }}"' +
            ' target="_blank">{{ suggestion }}</a>' +
        '</li>' +
      '</ul>',
    link: function (scope) {
      scope.$createObservableFunction('update')
        .debounce(400)
        .map(keyword => keyword.trim())
        .filter(keyword => keyword.length > 0)
        .distinctUntilChanged()
        .map(keyword => searchGitHub(keyword))
        .switch()
        .digest(scope, 'suggestions')
        .subscribe();
    }
  };
  
  function searchGitHub(term) {
    var apiUrl = 'https://api.github.com/search/users?q=';
    
    return Rx.Observable.create(function (observer) {  
      fetch(apiUrl + term)
        .then(res => res.json())
        .then(json => {
          observer.onNext(json);
          observer.onCompleted();
        }).catch(observer.onError);
      })
      .map(json => {
        return json.items.map(item => item.login);
      });
  }
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
cvuorinenpro
0viewers