Last active
August 29, 2015 13:59
-
-
Save mizanRahman/10712518 to your computer and use it in GitHub Desktop.
Angularjs: backend query with $resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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"> </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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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