javascript - Angular.js and DAO pattern -
first of must admit quite new angular.js , haven't used new generation js framework backbone or knockout before. i'm creating application communicates server using restful api. dug lot angular documentation , blog notes right.
i found examples $resource. looks pretty good: many built-in methods, when design rest interface don't have write more.
but (and whole team too) more used javaee way of thinking model layer: lightweight model classes (pojo, etc), dao classes persisting , fetching model , optionally service layer between dao , controllers. on other hand in angular $resource creates more resembling active record.
i've come 2 ways how implement dao pattern in angular:
- writing scratch, going down $http abstraction level. i'd implement every dao method $http call, take care errors.
- using $resource objects lightweight model classes , passing them daos unit responsible calling actions .$save() on them. of course cannot prevent calling in different place, solution such convention enough me.
second way looks better me because of reusing existing code. $resource has nice behaviour of promise object , happy if don't have implement myself.
so main question: active record approach way of doing data access right in angular, backbone , other tools that? maybe have tried incorporate similar solution more resembling dao in code , can share thoughts it?
and second question: $resource object sufficient when comes dealing errors, connection losses , different problems? worth use $resource having in mind or it's better start lower level $http.
i @ beginning of project , know decision may affect whole project life later, want choose best.
i totally agree. here way it:
bankapp.factory("customerrepository", function ($resource) { var customerrepository = $resource("rest/customers/:id", {id:'@id'}, {'update': {method:'put'}}); // can add addition repository $http calls onto // customerrepository getting count , other stuff. return customerrepository; });
then can inject customerrepository ever need it. example:
bankapp.controller("bankcontroller", function ($scope, customerrepository) { $scope.customers = customerrepository.query(); $scope.createcustomer = function () { customerrepository.save($scope.customer, function (customer) { ... }); }; ... $scope.savecustomer = function () { customerrepository.update($scope.customer, function (customer) { ... }); }; });