create separate controller for confessions

angular
Matt Huntington 10 years ago
parent cd06597fdd
commit 30b2173fa3

@ -20,9 +20,8 @@ app.controller('TransgressionsController', ['$http', function($http){
// Sin Types for Select in HTML // Sin Types for Select in HTML
this.SIN_TYPES = [ 'Gluttony', 'Greed', 'Anger', 'Pride', 'Lust', 'Sloth', 'Envy' ]; this.SIN_TYPES = [ 'Gluttony', 'Greed', 'Anger', 'Pride', 'Lust', 'Sloth', 'Envy' ];
this.newTransgressionSinType = 'Gluttony'; this.newTransgressionSinType = 'Gluttony';
//this fetches transgression data and adds it to controller //this fetches transgression data and adds it to controller
var getTransgressions = function(){ this.getTransgressions = function(){
// get transgressions for current User // get transgressions for current User
$http.get('/transgressions').success(function(data){ $http.get('/transgressions').success(function(data){
//just add the transgressions to the controller, data comes back with sinner as well //just add the transgressions to the controller, data comes back with sinner as well
@ -30,7 +29,7 @@ app.controller('TransgressionsController', ['$http', function($http){
}); });
} }
//fetch transgression data for current user as TransgressionsController initializes //fetch transgression data for current user as TransgressionsController initializes
getTransgressions(); this.getTransgressions();
// create a transgression // create a transgression
this.createTransgression = function(){ this.createTransgression = function(){
@ -60,14 +59,19 @@ app.controller('TransgressionsController', ['$http', function($http){
controller.current_user_transgressions.pop(); controller.current_user_transgressions.pop();
controller.current_user_transgressions.push(data.transgression); controller.current_user_transgressions.push(data.transgression);
//...and begin refreshing transgression data //...and begin refreshing transgression data
getTransgressions(); controller.getTransgressions();
}); });
} }
}]);
app.controller('ConfessionsController', ['$http', '$scope', function($http, $scope){
//get authenticity_token from DOM (rails injects it on load)
var authenticity_token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
// create a confession for a transgression identified by transgression_id // create a confession.!
this.createConfession = function(transgression_id){ this.createConfession = function(){
//AJAX call using parameter that identifies transgression //AJAX call using parameter that identifies transgression
$http.post('/transgressions/'+transgression_id+'/confessions', { $http.post('/transgressions/'+$scope.$parent.transgression.id+'/confessions', {
//include authenticity_token //include authenticity_token
authenticity_token: authenticity_token, authenticity_token: authenticity_token,
confession: { confession: {
@ -76,7 +80,7 @@ app.controller('TransgressionsController', ['$http', function($http){
} }
}).success(function(data){ }).success(function(data){
//refresh transgression data once POST is complete //refresh transgression data once POST is complete
getTransgressions(); $scope.$parent.transgressionsCtrl.getTransgressions();
}); });
} }
}]); }]);

@ -9,7 +9,9 @@
Sin Type: {{transgression.sin_type}} Sin Type: {{transgression.sin_type}}
<br/> <br/>
Description: {{transgression.description}} Description: {{transgression.description}}
<h3>Show me what you got:</h3> <div ng-controller="ConfessionsController as confessionsCtrl">
{{confessionsCtrl.bar}}
<h3>Show me what you got (confessions):</h3>
<ul> <ul>
<li ng-repeat="confession in transgression.confessions"> <li ng-repeat="confession in transgression.confessions">
What happened: {{confession.description}} What happened: {{confession.description}}
@ -19,15 +21,16 @@
</ul> </ul>
<!-- Confessions go are displayed/created --> <!-- Confessions go are displayed/created -->
<h3>Add a confession (yum):</h3> <h3>Add a confession (yum):</h3>
<form ng-submit="transgressionsCtrl.createConfession(transgression.id)"> <form ng-submit="confessionsCtrl.createConfession()">
<input type="text" ng-model="transgressionsCtrl.newConfessionDescription" placeholder="How did you please me?"/> <input type="text" ng-model="confessionsCtrl.newConfessionDescription" placeholder="How did you please me?"/>
<br/> <br/>
<input type="text" ng-model="transgressionsCtrl.newConfessionDate" placeholder="When did it happen?"/> <input type="text" ng-model="confessionsCtrl.newConfessionDate" placeholder="When did it happen?"/>
<br/> <br/>
<input type="submit" value="YESSS!" /> <input type="submit" value="YESSS!" />
<br/> <br/>
<br/> <br/>
</form> </form>
</div>
</li> </li>
</ul> </ul>

Loading…
Cancel
Save