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,23 +59,28 @@ 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();
}); });
} }
}]);
// create a confession for a transgression identified by transgression_id app.controller('ConfessionsController', ['$http', '$scope', function($http, $scope){
this.createConfession = function(transgression_id){ //get authenticity_token from DOM (rails injects it on load)
//AJAX call using parameter that identifies transgression var authenticity_token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
$http.post('/transgressions/'+transgression_id+'/confessions', {
//include authenticity_token // create a confession.!
authenticity_token: authenticity_token, this.createConfession = function(){
confession: { //AJAX call using parameter that identifies transgression
description: this.newConfessionDescription, $http.post('/transgressions/'+$scope.$parent.transgression.id+'/confessions', {
occurred_at: this.newConfessionDate //include authenticity_token
} authenticity_token: authenticity_token,
}).success(function(data){ confession: {
//refresh transgression data once POST is complete description: this.newConfessionDescription,
getTransgressions(); occurred_at: this.newConfessionDate
}); }
}).success(function(data){
//refresh transgression data once POST is complete
$scope.$parent.transgressionsCtrl.getTransgressions();
});
} }
}]); }]);

@ -9,25 +9,28 @@
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">
<ul> {{confessionsCtrl.bar}}
<li ng-repeat="confession in transgression.confessions"> <h3>Show me what you got (confessions):</h3>
What happened: {{confession.description}} <ul>
<br/> <li ng-repeat="confession in transgression.confessions">
When: {{confession.occurred_at}} What happened: {{confession.description}}
</li> <br/>
</ul> When: {{confession.occurred_at}}
<!-- Confessions go are displayed/created --> </li>
<h3>Add a confession (yum):</h3> </ul>
<form ng-submit="transgressionsCtrl.createConfession(transgression.id)"> <!-- Confessions go are displayed/created -->
<input type="text" ng-model="transgressionsCtrl.newConfessionDescription" placeholder="How did you please me?"/> <h3>Add a confession (yum):</h3>
<br/> <form ng-submit="confessionsCtrl.createConfession()">
<input type="text" ng-model="transgressionsCtrl.newConfessionDate" placeholder="When did it happen?"/> <input type="text" ng-model="confessionsCtrl.newConfessionDescription" placeholder="How did you please me?"/>
<br/> <br/>
<input type="submit" value="YESSS!" /> <input type="text" ng-model="confessionsCtrl.newConfessionDate" placeholder="When did it happen?"/>
<br/> <br/>
<br/> <input type="submit" value="YESSS!" />
</form> <br/>
<br/>
</form>
</div>
</li> </li>
</ul> </ul>

Loading…
Cancel
Save