Skip to content

Instantly share code, notes, and snippets.

@mizanRahman
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizanRahman/10712518 to your computer and use it in GitHub Desktop.
Save mizanRahman/10712518 to your computer and use it in GitHub Desktop.
Angularjs: backend query with $resource
<div class="row">
<div class="col-xs-24">
<div class="panel panel-blue">
<div class="panel-heading">
<span class="panel-title">Issues</span>
<a ng-href="#assets" class="btn btn-custom-orange pull-right"><small><span class="icon-expand">&nbsp</span></small>Expand</a>
</div>
<div class="panel-body">
<div ng-controller="MyCtrl1" style="color:#555;">
<table class="table">
<tr>
<th>Id</th>
<th>State</th>
<th>Reporter</th>
</tr>
<tr ng-repeat="gist in data.gists">
<td><a class="btn btn-custom-sky" ng-click="showIssue(gist.id)">{{gist.id}}</a></td>
<td>{{gist.description}}</td>
<td>{{gist.user.login}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
angular.module('relitagApp')
.factory('Gist', function($resource){
var apiUrl = 'https://api.github.com';
var Gist = $resource(apiUrl + '/gists/:id', {
},{
//creating a custom method
'update': { method:'PUT' }
});
return Gist ;
})
.controller('MyCtrl1', ['$scope', 'Gist', function($scope, Gist) {
// Instantiate an object to store your scope data in (Best Practices)
$scope.data = {};
Gist.query(function(response) {
// Assign the response INSIDE the callback
$scope.data.gists = response;
//console.log(response);
});
$scope.showIssue = function(gistId){
var gist = Gist.get({id: gistId},function(){
//alert(gist.files);
for(var filename in gist.files) {
console.log(filename);
$scope.data.gist = gist.files[filename].content;
break;
}
gist.description = "changed the description";
Gist.update({id: gistId});
var newGist = Gist.get({id: gistId},function(){
console.log(newGist.description);
});
});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment