diff --git a/build/html/form.html b/build/html/form.html index a309897..8a0a8d7 100644 --- a/build/html/form.html +++ b/build/html/form.html @@ -22,15 +22,13 @@

Add or Edit A Blogpost

-
- Gist Description - {{vm.gist.description}} + + + - - - - Public - Gist Username - Date - + + + + +
diff --git a/src/html/blog.html b/src/html/blog.html index 73418c8..4475923 100644 --- a/src/html/blog.html +++ b/src/html/blog.html @@ -36,7 +36,7 @@

{{ gists.id }}

View All - Edit + Edit diff --git a/src/html/form.html b/src/html/form.html index a309897..8a0a8d7 100644 --- a/src/html/form.html +++ b/src/html/form.html @@ -22,15 +22,13 @@

Add or Edit A Blogpost

-
- Gist Description - {{vm.gist.description}} + + + - - - - Public - Gist Username - Date - + + + + +
diff --git a/src/js/gist-form-ctrl.js b/src/js/gist-form-ctrl.js index 7666f0c..8c39366 100644 --- a/src/js/gist-form-ctrl.js +++ b/src/js/gist-form-ctrl.js @@ -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 =======// - +}()); diff --git a/src/js/gists-app.js b/src/js/gists-app.js index 854c327..1c26eaa 100644 --- a/src/js/gists-app.js +++ b/src/js/gists-app.js @@ -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", });