Skip to content

Commit

Permalink
Start moving towards $resource
Browse files Browse the repository at this point in the history
Added $resource for User and Site
Updated Sites/SiteController to use new resources
  • Loading branch information
gmjosack committed Jan 15, 2015
1 parent aee8d04 commit 5ff5689
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 29 deletions.
2 changes: 1 addition & 1 deletion nsot/static/js/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function() {
"use strict";

var app = angular.module("nsotApp", ["ngRoute"]);
var app = angular.module("nsotApp", ["ngRoute", "ngResource"]);

app.config(function($interpolateProvider){
$interpolateProvider.startSymbol("[[");
Expand Down
56 changes: 29 additions & 27 deletions nsot/static/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,58 +36,60 @@
}]);

app.controller("SitesController", [
"$scope", "$http", "$q", "$location",
function($scope, $http, $q, $location) {
"$scope", "$q", "$location", "Site", "User",
function($scope, $q, $location, Site, User) {

$scope.loading = true;
$scope.user = {};
$scope.sites = [];
$scope.site = {};
$scope.user = new User();
$scope.site = new Site();
$scope.error = null;

$q.all([
$http.get("/api/users/0"),
$http.get("/api/sites")
User.get({id: 0}).$promise,
Site.query().$promise
]).then(function(results){
$scope.user = results[0].data.data.user;
$scope.sites = results[1].data.data.sites;
$scope.user = results[0];
$scope.sites = results[1];

$scope.loading = false;
});

$scope.createSite = function(site){
$http.post("/api/sites", site).success(function(data){
$scope.site.$save(function(data){
var site = data.data.site;
$location.path("/sites/" + site.id);
}).error(function(data){
$scope.error = data.error;
}, function(data){
$scope.error = data.data.error;
});
};
}]);

app.controller("SiteController", [
"$scope", "$http", "$route", "$location", "$q", "$routeParams",
function($scope, $http, $route, $location, $q, $routeParams) {
"$scope", "$route", "$location", "$q", "$routeParams", "Site", "User",
function($scope, $route, $location, $q, $routeParams, Site, User) {

$scope.loading = true;
$scope.user = {};
$scope.site = {};
$scope.user = new User();
$scope.site = new Site();
$scope.admin = false;
$scope.updateError = null;
$scope.deleteError = null;

var siteId = $routeParams.siteId;

$q.all([
$http.get("/api/users/0"),
$http.get("/api/sites/" + $routeParams.siteId)
User.get({id: 0}).$promise,
Site.get({id: siteId}).$promise,
]).then(function(results){
$scope.user = results[0].data.data.user;
$scope.site = results[1].data.data.site;
var permissions = $scope.user.permissions[$routeParams.siteId] || {};
$scope.user = results[0];
$scope.site = results[1];

var permissions = $scope.user.permissions[siteId] || {};
permissions = permissions.permissions || [];
$scope.admin = _.any(permissions, function(value){
return _.contains(["admin"], value);
});

$scope.loading = false;
}, function(data){
if (data.status === 404) {
Expand All @@ -97,18 +99,18 @@
});

$scope.updateSite = function(site){
$http.put("/api/sites/" + $routeParams.siteId, site).success(function(data){
$scope.site.$update(function(data){
$route.reload();
}).error(function(data){
$scope.updateError = data.error;
}, function(data){
$scope.updateError = data.data.error;
});
};

$scope.deleteSite = function(site){
$http.delete("/api/sites/" + $routeParams.siteId, site).success(function(data){
$scope.site.$delete(function(data){
$location.path("/sites");
}).error(function(data){
$scope.deleteError = data.error;
}, function(data){
$scope.deleteError = data.data.error;
});
};

Expand Down
46 changes: 46 additions & 0 deletions nsot/static/js/services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
(function() {
"use strict";

var app = angular.module("nsotApp");

app.factory("Site", ["$resource", function($resource){
return $resource("/api/sites/:id", { id: "@id" }, {
query: {
method: "GET", isArray: true,
transformResponse: function(data){
data = angular.fromJson(data);
return data.data.sites;
}
},
get: {
method: "GET",
transformResponse: function(data){
data = angular.fromJson(data);
return data.data.site;
}
},
update: { method: "PUT" }
});
}]);

app.factory("User", ["$resource", function($resource){
return $resource("/api/users/:id", { id: "@id" }, {
query: {
method: "GET", isArray: true,
transformResponse: function(data){
data = angular.fromJson(data);
return data.data.users;
}
},
get: {
method: "GET",
transformResponse: function(data){
data = angular.fromJson(data);
return data.data.user;
}
},
update: { method: "PUT" }
});
}]);

})();
4 changes: 3 additions & 1 deletion nsot/templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@
</div>

<script src="{{cdnjs_prefix}}/ajax/libs/angular.js/1.3.8/angular.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/angular.js/1.3.8/angular-route.min.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/angular.js/1.3.8/angular-route.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/angular.js/1.3.8/angular-resource.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="{{cdnjs_prefix}}/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script src="/static/js/nsot.js"></script>
<script src="/static/js/app.js"></script>
<script src="/static/js/services.js"></script>
<script src="/static/js/filters.js"></script>
<script src="/static/js/controllers.js"></script>
<script src="/static/js/directives.js"></script>
Expand Down

0 comments on commit 5ff5689

Please sign in to comment.