I only started learning ng-resource recently. I'm wondering if I am on the right track when it comes to converting my regular AngularJS application to one which uses ng-resource.
This is my old home.js
controller (without using ng-resource):
angular.module("HomeApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
BaseService.fetch.posts(function() {
self.posts = BaseService.posts;
self.cerrorMessages = BaseService.cerrorMessages;
});
self.like = function(id, postType) {
BaseService.like(id, postType, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
}]);
And this is my old base.js
(BaseService):
angular.module("BaseApp", [])
.factory("BaseService", ["$http", "$window", function($http, $window) {
var self = this;
self.posts = [];
self.cerrorMessages = [];
self.accessErrors = function(data) {
self.cerrorMessages = [];
for (prop in data) {
if (data.hasOwnProperty(prop)){
self.cerrorMessages.push(data[prop]);
}
}
};
self.fetch = {
posts: function(callback) {
$http.get('/posts/')
.then(function(response) {
self.posts = response.data;
callback();
}, function(response) {
self.accessErrors(response.data);
callback();
});
}
};
self.like = function(id, postType, callback) {
$http.post("/" + postType + "/" + id + "/like/")
.then(function(response) {
angular.forEach(self.posts, function(post, index, obj) {
if (post.id == id) {
post.usersVoted.push('voted');
post.voted=true;
};
});
}, function(response) {
self.accessErrors(response.data);
callback();
});
};
return self;
}]);
I was told that I can greatly benefit from using ng-resources since I have a RESTful backend. I tried converting the code above to start using ng-resource, and this is what I have now. This is my resources.js
:
angular.module('PostsResources', ['ngResource'])
.factory('PostsFactory', function($resource) {
return $resource('/posts/:postId', { postId:'@id' });
});
.factory('PostLikeFactory', function($resource) {
return $resource('/posts/:postId/like', { postId:'@id' });
});
And this is my controller (home.js
):
angular.module("HomePageApp", ["BaseApp", "PostsApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", "PostsResource", "PostLikeResource", function($http, $window, BaseService, PostsResource, PostLikeResource) {
var self = this;
function loadPosts() {
self.posts = PostsFactory.query(function(data) {},
function(error) {
BaseService.accessErrors(error.data);
self.cerrorMessages = BaseService.cerrorMessages;
}
);
};
self.like = function likePost() {
PostLikeFactory.save(function(data) {
angular.forEach(self.posts, function(post, index, obj) {
if (post.id == id) {
post.usersVoted.push('voted');
post.voted=true;
};
});
}, function(error) {
BaseService.accessErrors(error.data);
self.cerrorMessages = BaseService.cerrorMessages;
}
)};
And this is my BaseService (base.js
):
angular.module("BaseApp", [])
.factory("BaseService", ["$http", "$window", function($http, $window) {
var self = this;
self.posts = [];
self.cerrorMessages = [];
self.accessErrors = function(data) {
self.cerrorMessages = [];
for (prop in data) {
if (data.hasOwnProperty(prop)){
self.cerrorMessages.push(data[prop]);
}
}
};
return self;
}]);
Am I on the right track of converting a regular AngularJS app to using ng-resource? To me it seems like the amount of code required looks the same. Thanks in advance!