Skip to content

Instantly share code, notes, and snippets.

@daha
Created July 4, 2012 15:49
Show Gist options
  • Save daha/3047993 to your computer and use it in GitHub Desktop.
Save daha/3047993 to your computer and use it in GitHub Desktop.
AngularJS: GitHub Contributors

GitHub Contributors

This is little Angular.js app.

It uses a few Angular "features":

  • Routes: Display different content from a templates depending of the url.
  • Location: The url is updated and the back button should work.
  • Resources: A custom resource has been written to create the queries towards GitHub.

It has been inspired by the example app GitHub Contributors in YUI 3.5.

View the app on bl.ocks.org

// Copyright (c) 2012, David Haglund
/*globals angular,SearchCtrl,UserCtrl,RepoCtrl */
// Declare app level module which depends on filters, and services
angular.module('ghContrib', ['ghContrib.services']).
config(['$routeProvider', function ($routeProvider) {
'use strict';
$routeProvider.when('/', {
templateUrl: 'partials/search.html',
controller: SearchCtrl
});
$routeProvider.when('/github/:user/', {
templateUrl: 'partials/user.html',
controller: UserCtrl
});
$routeProvider.when('/github/:user/:repo/', {
templateUrl: 'partials/repo.html',
controller: RepoCtrl
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
// Copyright (c) 2012, David Haglund
/*globals angular */
/* Controllers */
function SearchCtrl($scope, $location) {
'use strict';
$scope.userSearch = function () {
$location.path('/github/' + $scope.user + "/");
};
}
SearchCtrl.$inject = ['$scope', '$location'];
function UserCtrl($scope, $location, $routeParams, githubResource) {
'use strict';
$scope.repos = githubResource.get({user: $routeParams.user});
$scope.user = $routeParams.user;
$scope.repoSearch = function (repo) {
$location.path(['', 'github', $scope.user, repo, '' ].join('/'));
};
}
UserCtrl.$inject = ['$scope', '$location', '$routeParams', 'githubResource'];
function RepoCtrl($scope, $routeParams, githubResource) {
'use strict';
$scope.contributors = githubResource.get({
"query": "repos",
"user": $routeParams.user,
"repo": $routeParams.repo,
"spec": "contributors"
});
}
RepoCtrl.$inject = ['$scope', '$routeParams', 'githubResource'];
<!doctype html>
<html lang="en" ng-app="ghContrib">
<head>
<meta charset="utf-8">
<title>
GitHub Contributors
</title>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
</head>
<body>
<div ng-view>
</div>
<script src="http://code.angularjs.org/1.0.1/angular-1.0.1.min.js"></script>
<script src="http://code.angularjs.org/1.0.1/angular-resource-1.0.1.min.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
</body>
</html>
// Copyright (c) 2012, David Haglund
/*globals angular,SearchCtrl,UserCtrl,RepoCtrl */
// Declare app level module which depends on filters, and services
angular.module('ghContrib', ['ghContrib.services']).
config(['$routeProvider', function ($routeProvider) {
'use strict';
$routeProvider.when('/', {
templateUrl: 'partials/search.html',
controller: SearchCtrl
});
$routeProvider.when('/github/:user/', {
templateUrl: 'partials/user.html',
controller: UserCtrl
});
$routeProvider.when('/github/:user/:repo/', {
templateUrl: 'partials/repo.html',
controller: RepoCtrl
});
$routeProvider.otherwise({
redirectTo: '/'
});
}]);
// Copyright (c) 2012, David Haglund
/*globals angular */
/* Controllers */
function SearchCtrl($scope, $location) {
'use strict';
$scope.userSearch = function () {
$location.path('/github/' + $scope.user + "/");
};
}
SearchCtrl.$inject = ['$scope', '$location'];
function UserCtrl($scope, $location, $routeParams, githubResource) {
'use strict';
$scope.repos = githubResource.get({user: $routeParams.user});
$scope.user = $routeParams.user;
$scope.repoSearch = function (repo) {
$location.path(['', 'github', $scope.user, repo, '' ].join('/'));
};
}
UserCtrl.$inject = ['$scope', '$location', '$routeParams', 'githubResource'];
function RepoCtrl($scope, $routeParams, githubResource) {
'use strict';
$scope.contributors = githubResource.get({
"query": "repos",
"user": $routeParams.user,
"repo": $routeParams.repo,
"spec": "contributors"
});
}
RepoCtrl.$inject = ['$scope', '$routeParams', 'githubResource'];
/*globals angular */
/* Services */
angular.module('ghContrib.services', ['ngResource'], function ($provide) {
'use strict';
$provide.factory('githubResource', function ($resource) {
return $resource('https://api.github.com/:query/:user/:repo/:spec', {
'query': 'users',
'user': 'angular',
'repo': 'repos',
'spec': '',
'callback': 'JSON_CALLBACK',
'per_page': 100
}, {
'get': {
'method': 'JSONP'
}
});
});
});
Copyright (c) 2012, David Haglund
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<table class="table table-striped">
<tr>
<th>Login</th>
<th>Url</th>
<th>Contributions</th>
</tr>
<tr ng-repeat="contributor in contributors.data">
<td>
{{contributor.login}}
</td>
<td>
{{contributor.url}}
</td>
<td>
{{contributor.contributions}}
</td>
</tr>
</table>
<form class="form-horizontal" ng-submit="userSearch()">
<input type="text" ng-model="user" placeholder="github username">
<button class="btn">
<i class="icon-search"></i>
Show Repos
</button>
</form>
<table class="table table-striped" >
<tr ng-repeat="repo in repos.data">
<td>
<a ng-click="$parent.repoSearch(repo.name)">{{repo.name}}</a>
</td>
</tr>
</table>
<hr/>
<table class="table table-striped">
<tr>
<th>Login</th>
<th>Url</th>
<th>Contributions</th>
</tr>
<tr ng-repeat="contributor in contributors.data">
<td>
{{contributor.login}}
</td>
<td>
{{contributor.url}}
</td>
<td>
{{contributor.contributions}}
</td>
</tr>
</table>
<form class="form-horizontal" ng-submit="userSearch()">
<input type="text" ng-model="user" placeholder="github username">
<button class="btn">
<i class="icon-search"></i>
Show Repos
</button>
</form>
/*globals angular */
/* Services */
angular.module('ghContrib.services', ['ngResource'], function ($provide) {
'use strict';
$provide.factory('githubResource', function ($resource) {
return $resource('https://api.github.com/:query/:user/:repo/:spec', {
'query': 'users',
'user': 'angular',
'repo': 'repos',
'spec': '',
'callback': 'JSON_CALLBACK',
'per_page': 100
}, {
'get': {
'method': 'JSONP'
}
});
});
});
<table class="table table-striped" >
<tr ng-repeat="repo in repos.data">
<td>
<a ng-click="$parent.repoSearch(repo.name)">{{repo.name}}</a>
</td>
</tr>
</table>
<hr/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment