Closed ghost closed 8 years ago
Are you serious?
I mean i don't want to sound rude, but a simple CRUD service with laravel it's easiest thing you can do :confused: those API
and Toast
services are really optional, you can use whatever you want you want to use $http
or your own Restangular
service. it's really optional, they're just there for you to start work on your project without the need of configure Restangular
or the Toast
from angular material
@jadjoubran recommends using those exactly because they work already out of the box and because well Restagular
it's way better than the $http
from angular
Sorry I wrote a bit unclear, I'm not so familiar with angularjs. The link in your post talks mainly about laravel resource controllers and that's not what I meant. I have dozens of models in my app and I'm trying to find out if I can make my code more DRY with angularjs service or factory for making GET, POST, PUT and DELETE requests to laravel controllers. I think this kind of factory with use of API and Toast services (maybe with dialog service) would be useful for users/projects(?)
It's even easier to do that on angular, especially with Restangular as simply as (asuming you are using this starter and the API service that it's included):
this.API.all(this.route).getList(params).$object;
this.API.one(this.route, id).get();
this.API.one(this.route, id).one(this.route + 'view').get();
this.API.put().$object;
this.API.all(this.route).post(newResource);
this.API.one(this.route, object.id).remove();
that's why i post that link, cause basically everything you need to create a CRUD service with laravel and angular are just the routes and the controllers from the REST API in laravel
Ok, thanks. Just found this: https://github.com/beeman/angular-restangular-crud/blob/master/client/app/scripts/factory.js Going to give it a try!
I tried the factory above and browser throws error:
Error: [$injector:unpr] Unknown provider: restangularProvider <- restangular <- ExPuolisoRepository
Any idea?
My AbstractFactory:
(function() {
'use strict';
// Source https://github.com/beeman/angular-restangular-crud
angular
.module('app.perukirjapalvelu.perukirja-forms')
.factory('AbstractRepository', AbstractRepository);
/* @ngInject */
function AbstractRepository(route, API) {
this.route = route;
this.API = API;
}
AbstractRepository.prototype = {
getList: function (params) {
return this.API.all(this.route).getList(params).$object;
},
get: function (id) {
return this.API.one(this.route, id).get();
},
getView: function (id) {
return this.API.one(this.route, id).one(this.route + 'view').get();
},
update: function (updatedResource) {
return updatedResource.put().$object;
},
create: function (newResource) {
return this.API.all(this.route).post(newResource);
},
remove: function (object) {
return this.API.one(this.route, object.id).remove();
}
};
AbstractRepository.extend = function (repository) {
repository.prototype = Object.create(AbstractRepository.prototype);
repository.prototype.constructor = repository;
};
return AbstractRepository;
})();
My ExPuolisoRepository:
// Source https://github.com/beeman/angular-restangular-crud
(function() {
'use strict';
angular
.module('app.perukirjapalvelu.perukirja-forms.ex-puoliso')
.factory('ExPuolisoRepository', ExPuolisoRepository);
/* @ngInject */
function ExPuolisoRepository(restangular, AbstractRepository) {
// function NoteRepository() {
// AbstractRepository.call(this, restangular, 'notes');
// }
// function ExPuolisotRepository() {
AbstractRepository.call(this, restangular, 'exPuolisot');
// }
AbstractRepository.extend(ExPuolisoRepository);
return new ExPuolisoRepository();
}
})();
My controller:
(function() {
'use strict';
// Source https://github.com/beeman/angular-restangular-crud
angular
.module('app.perukirjapalvelu.perukirja-forms.ex-puoliso.create')
.controller('CreateExPuolisoController', CreateExPuolisoController);
/* @ngInject */
function CreateExPuolisoController($state, ExPuolisoRepository, ToastService) {
var vm = this;
vm.$state = $state;
vm.ExPuolisoRepository = ExPuolisoRepository;
vm.ToastService = ToastService;
vm.save = function () {
ExPuolisoRepository.create(vm.exPuoliso).then(function () {
vm.ToastService.show('Entinen puoliso tallennettu!');
vm.$state.go('triangular.perukirja-forms.ex-puoliso.index');
});
};
}
})();
Sorry bro, can't help you, those files are not from the starter, only you know where are those from, and you should ask in their repository, although you are triying to use ToastService
and API
services, wich are from this starter, but there's no way they'll work as you are triying to use them since those files are in ecmascript 5
and this startes uses ecmascript 6
and well those this
doesn't have access to the same this
scope you should use an arrow function (() => {}
) but they're not supported on ecmascript 5
Ok, thanks! I use previous version of Toast and API services and they work fine in login and register modules..
Yes, because they're are part of the starter, those functions as i say are not
Thanks, I keep on searching.. =)
@regisaih what do you want to accomplish? for a CRUD service all you have to do it's have a laravel route, with a controller attach to it that return an array with json data on it and in any angular controller of the starter you''ll inject the API service and do what you want get, post, put, patch, destroy
have you read the docs?
Hi @flick36 I have lot of models for which I have to write angular crud controllers. I'm trying to keep these controllers as thin as possible and avoid repeating by moving 'dry' code to factory. But I'm not sure if this is beneficial in the end. So I'm trying to find out this by testing factories in practice. Yep have read the docs.
Thanks for great starter template! I use it in my project which is essentially a crud app. I'm planning to implement crud service for basic crud operations, but I'm not sure of it's benefits and which is best way to implement it. Do I have to integrate it somehow to your API and Toast services? And are you going to write such crud service out of box?