Skip to content

Commit

Permalink
Merge pull request #21 from toalina/gist-form
Browse files Browse the repository at this point in the history
map fields to forms and working on form controller
  • Loading branch information
toalina committed Oct 16, 2015
2 parents dbd46f9 + b7894af commit 49cb75a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 75 deletions.
18 changes: 8 additions & 10 deletions build/html/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ <h1>Add or Edit A Blogpost</h1>
</div>
</div>
<!-- Form -->
<form name="gistForm" ng-submit="vm.save()" ng-controller="GistFormCtrl">
Gist Description
<input type="text" ng-model="vm.gist.description" placeholder="Description">{{vm.gist.description}}</input>
<form name="gistForm" ng-submit="save()" ng-controller="GistFormCtrl">
<input type="text" ng-model="gist.description" placeholder="Description"/>
<input type="text" ng-model="newContent">

<input type="text" ng-model="vm.newContent">

<textarea type="text" ng-model="vm.gist.files" placeholder="Content">Gist Files</textarea>
<input type="checkbox" ng-model="vm.gist.public" value="Public">Public</input>
<input type="text" ng-model="vm.gist.owner.login" placeholder="Author">Gist Username</input>
<input type="date" ng-model="vm.gist.created_at" placeholder="Date">Date</input>
<input class="btn-primary-alt" type="submit" name="Submit">
<textarea type="text" ng-model="gist.files" placeholder="Content">Gist Files</textarea>
<input type="checkbox" ng-model="gist.public" value="Public"/>
<input type="text" ng-model="gist.owner.login" placeholder="Author"/>
<input type="text" ng-model="gist.created_at" placeholder="Date"/>
<input class="btn-primary-alt" type="submit" name="Submit"/>
</form>
2 changes: 1 addition & 1 deletion src/html/blog.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h1>{{ gists.id }}</h1>
</ul>

<a class="btn-primary" href="#/gists/">View All</a>
<a class= "btn-small" href="#/blogposts/{{vm.blogpost._id}}/edit">Edit</a>
<a class= "btn-small" href="#/gists/{{gist.id}}/edit">Edit</a>

</article>
</section>
18 changes: 8 additions & 10 deletions src/html/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ <h1>Add or Edit A Blogpost</h1>
</div>
</div>
<!-- Form -->
<form name="gistForm" ng-submit="vm.save()" ng-controller="GistFormCtrl">
Gist Description
<input type="text" ng-model="vm.gist.description" placeholder="Description">{{vm.gist.description}}</input>
<form name="gistForm" ng-submit="save()" ng-controller="GistFormCtrl">
<input type="text" ng-model="gist.description" placeholder="Description"/>
<input type="text" ng-model="newContent">

<input type="text" ng-model="vm.newContent">

<textarea type="text" ng-model="vm.gist.files" placeholder="Content">Gist Files</textarea>
<input type="checkbox" ng-model="vm.gist.public" value="Public">Public</input>
<input type="text" ng-model="vm.gist.owner.login" placeholder="Author">Gist Username</input>
<input type="date" ng-model="vm.gist.created_at" placeholder="Date">Date</input>
<input class="btn-primary-alt" type="submit" name="Submit">
<textarea type="text" ng-model="gist.files" placeholder="Content">Gist Files</textarea>
<input type="checkbox" ng-model="gist.public" value="Public"/>
<input type="text" ng-model="gist.owner.login" placeholder="Author"/>
<input type="text" ng-model="gist.created_at" placeholder="Date"/>
<input class="btn-primary-alt" type="submit" name="Submit"/>
</form>
73 changes: 19 additions & 54 deletions src/js/gist-form-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,42 @@ require("./gists-app.js");
(function() {
'use strict';

angular.module("gisty").controller("GistFormCtrl", ["GistService", "$routeParams", "$location", function (GistService, $routeParams, $location) {
angular.module("gisty").controller("GistFormCtrl", ["GistService", "$routeParams", "$location", "$scope", function(GistService, $routeParams, $location, $scope){

var vm = this;
$scope.save = saveBlog;

vm.save = saveGist;

vm.gist = {};
$scope.gist = {};

start();

// IF statement only works when edit
function start() {

vm.gist.created_at = new Date(Date.now());
if ($routeParams.id) {
BlogpostService.get($routeParams.id).then(function (resp) {

vm.gist = resp.data;
vm.gist.created_at = vm.gist.created_at || new Date(Date.now());
});
}
GistService.get($routeParams.id)
.then(successHandler, errorHandler);
}

function successHandler(response) {
vm.gist = response.data;
vm.newFilename = Object.keys(vm.gist.files)[0];
vm.newContent = vm.gist.files[vm.newFilename].content;

$log.info("response", response);
$log.info(vm.gist);
var data = response.data;
console.log(data);
$scope.gist = response.data;
// $log.info('response', response);
} // object comes with property of data

function errorHandler(response) {
$scope.error = response.data;
// $log.error('response', response);
}

function errorHandler (response) {
$log.error("response", response);
} if ($routeParams.gist_id) {
GistService.get($routeParams.gist_id).then(function(){
console.log(response);
});
}


function saveGist () {

function saveBlog () {
var method;

method = $routeParams.gist._id ? "update" : "create";

vm.gist.files = {};

if (!vm.oldFilename) {
vm.gist.files[vm.oldFilename] = {
filename: 'some name'

}
}

vm.gist.files[vm.newFilename] = {
"content": "vm.newContent"
};

GistService[method](vm.gist).then(successHandler, errorHandler);


GistService[method](vm.gist).then(function (resp) {
$location.path("/gists/" + resp.data._id);
method = $routeParams.id ? "update" : "create";
GistService[method]($scope.gist).then(function (resp) {
$location.path("/gists/" + resp.data.id);
});

}
}]);

}()); // ======= END OF IIFE =======//

}());


4 changes: 4 additions & 0 deletions src/js/gists-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var route = require('angular-route');
templateUrl: "html/blog.html",
controller: "SingleGistCtrl",
})
.when("/gists/:id/edit", {
templateUrl: "html/form.html",
controller: "GistFormCtrl",
})
.otherwise({
redirectTo: "/gists",
});
Expand Down

0 comments on commit 49cb75a

Please sign in to comment.