rest - How to pass model name to angularJS restful api -
i new angularjs, have used backbone while now.
i want create reusable restful api can pass model name can use different models.
at moment have:
angular.module('healthplanapiservices', ['ngresource']) .factory('api', function($resource) { return $resource(base_url + 'api/:object/:id', { id : '@id', object : 'actions' }, { query : { method : 'get', isarray : true, callback : "json_callback" }, save : { method : '@id' ? 'put' : 'post', isarray : true, callback : "json_callback" } }); });
... sets model 'actions'. in controller use:
// collection api.query(function(r) { $scope.actions = r; }); $scope.toggledone = function(a) { a.done = !a.done; //save item api.save(a); }
that's fine, how pass model name ('actions' in case) each model type: e.g., instead of putting in factory function so:
id : '@id', object : 'actions'
... rather more like:
var actionapi = api.setobject('actions'); actionapi.query(function(r) { $scope.actions = r; });
update: figured out way. may not best, work. other suggestions welcome! add 'object' attribute model:
api.query({object: 'actions'}, function(r) { $scope.actions = r; angular.foreach($scope.actions, function(a) { a.object = 'actions' }); }); api.save(a);// works
you may want try https://github.com/klederson/modelcore ( modelcore )
its easy model , structure , of course ... use.
model.$find({ filter : "john" },{},true); //to find query parameters model.$get(1); //to retreive user id 1 model.$save() model.$deelte();
and on.. checkout documentation
Comments
Post a Comment