First off I am very new to Angular so forgive me first of all :)
I am coming from a php MVC background where a Controller is specific to
that Model and the task it performs.
UserController.php would interact with User.php Model to CRUD the User in
essence.
Looking at Google tutorial searches and guides they are all so primitive
where everything is just basic and a single controller approach, just the
basics.
Read a few books and same thing. Usually like "we wont get into that, for
the sake of this guide we will put everything in app.js"
So I originally figured I would in Angular have a controller for each to
match but after a day it all seemed repetitive so I created a CRUD service
which would handle all requests for each action performed.
With calls like :
index : function(url) {
return $http.get(urls.BASE_API + url);
},
//pull a specific record vs full data set
get : function(url, id) {
return $http.get(urls.BASE_API + url + id);
},
save : function(url, formData) {
return $http({
method: 'POST',
url: urls.BASE_API + url,
headers: { 'Content-Type' :
'application/x-www-form-urlencoded' },
data: $.param(formData)
});
},
update : function(url, formData) {
return $http({
method: 'PUT',
url: urls.BASE_API + url,
headers: { 'Content-Type' :
'application/x-www-form-urlencoded' },
data: $.param(formData)
});
},
// destroy a record
destroy : function(url) {
return $http.delete(urls.BASE_API + url);
}
So I only need to pass the url endpoint rather than typing out the same
functionality over and over.
So basically I ended up with (right now 5) controllers
IndexableController.js //for showing index of records not with pagination
UpdatableController.js //for updating
CreatableController.js //for creating
DeletableController.js //for deleing records
PageableController.js // for pages requiring pagination
And say a form under the UpdatableController a form has has a
$scope.submitForm(); call on submit
$scope.submitForm = function() {
Crud.update(path, $scope.data.formData)
.then(function(response) {
if(response.data.status == 'success'){
//decide how to handle success to do what after saved???
$scope.errors = {}
$state.transitionTo('dashboard', {}, {
reload: true,
inherit: false,
notify: true
});
} else {
$scope.errors = response.data.errors;
console.log($scope.errors);
}
},
function(response) {
//console.log(response);
});
};
So far it works, but being new am I doing this right?
Whats wrong?
Improve how?
I greatly appreciate any help, tips pointers and the time everyone takes to
help out....
Thanks,
Dave
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.