Created
April 9, 2014 16:48
-
-
Save rchrand/10290936 to your computer and use it in GitHub Desktop.
AngularJS update problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var blogApp = angular.module('blogApp', [ | |
| "ngRoute", | |
| "blogControllers", | |
| "blogFilters", | |
| "blogServices"]); | |
| blogApp.config(['$routeProvider', | |
| function($routeProvider) { | |
| $routeProvider. | |
| when('/', { | |
| templateUrl: '/partials/posts', | |
| controller: 'PostController' | |
| }). | |
| when('/posts/:id', { | |
| templateUrl: '/partials/post', | |
| controller: 'ShowController' | |
| }). | |
| when('/admin', { | |
| templateUrl: '/partials/admin', | |
| controller: 'AdminController' | |
| }). | |
| when('/admin/update/:id', { | |
| templateUrl: '/partials/update', | |
| controller: 'AdminController' | |
| }). | |
| when('/admin/create', { | |
| templateUrl: '/partials/form', | |
| controller: 'AdminController' | |
| }). | |
| otherwise({ | |
| redirectTo: '/' | |
| }); | |
| }]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongoose = require('mongoose'); | |
| var Post = mongoose.model('Post'); | |
| exports.update = function(req, res){ | |
| var param = req.params.id; | |
| Post.findOne({_id: params}, function(err, post){ | |
| post.title = req.body.title; | |
| post.content = req.body.content; | |
| post.save(); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| 'use strict'; | |
| // ... | |
| blogControllers.controller('UpdateController', ["$scope", "PostsFactory", "$routeParams", "$location", | |
| function($scope, PostsFactory, $routeParams, $location){ | |
| $scope.post = PostsFactory.get({'id': $routeParams.id}); | |
| var post = $scope.post; | |
| var $id = post._id; | |
| $scope.update = function(){ | |
| PostsFactory.update({_id: $id}, post); | |
| $location.path("/admin"); | |
| }; | |
| }]); | |
| // ... | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongoose = require('mongoose'); | |
| var Schema = mongoose.Schema; | |
| var ObjectId = Schema.ObjectId; | |
| var autoIncrement = require('mongoose-auto-increment'); | |
| var connection = mongoose.connect('mongodb://localhost/my-blog'); | |
| autoIncrement.initialize(connection); | |
| var Post = new Schema({ | |
| title: {type: String, required: true }, | |
| content: {type: String, required: true}, | |
| created_at: {type: Date, default: Date.now} | |
| }); | |
| Post.plugin(autoIncrement.plugin, 'Book'); | |
| mongoose.model('Post', Post); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var model = require('./models/post.js'); | |
| var routes = require('./routes'); | |
| var api = require('./routes/api.js'); | |
| var express = require('express'); | |
| var http = require('http'); | |
| var path = require('path'); | |
| var app = express(); | |
| // all environments | |
| app.set('port', process.env.PORT || 3000); | |
| app.set('views', path.join(__dirname, 'views')); | |
| app.set('view engine', 'jade'); | |
| app.use(express.favicon("public/images/favicon.ico")); | |
| app.use(express.logger('dev')); | |
| app.use(express.json()); | |
| app.use(express.urlencoded()); | |
| app.use(express.methodOverride()); | |
| app.use(express.cookieParser('your secret here')); | |
| app.use(express.session()); | |
| app.use(app.router); | |
| app.use(require('less-middleware')(path.join(__dirname, 'public'))); | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| // development only | |
| if ('development' == app.get('env')) { | |
| app.use(express.errorHandler({ dumpExceptions: true, showStack: true})); | |
| } | |
| // Default routes | |
| app.get('/', routes.index); | |
| app.get('/partials/:filename', routes.partials); | |
| // API | |
| app.get('/api/posts', api.all); | |
| app.post('/api/posts', api.create); | |
| app.get('/api/posts/:id', api.show); | |
| app.put('/api/posts/:id', api.update); | |
| app.del('/api/posts/:id', api.remove); | |
| http.createServer(app).listen(app.get('port'), function(){ | |
| console.log('Magic on: ' + app.get('port')); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function() { | |
| 'use strict'; | |
| var blogServices = angular.module('blogServices', ['ngResource']); | |
| blogServices.factory('PostsFactory', ["$resource", | |
| function($resource){ | |
| return $resource('/api/posts/:id', {}, | |
| { | |
| update: { method: 'PUT'}, | |
| post: { method: 'POST'} | |
| }); | |
| }]); | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| section(data-ng-controller="UpdateController") | |
| form(role="form", data-ng-submit="update()") | |
| fieldset | |
| .form-body | |
| .field | |
| label Title: | |
| input(type='text', data-ng-model='post.title') | |
| .field | |
| label Content: | |
| textarea(data-ng-model='post.content') | |
| .form-footer | |
| button(type="submit") Submit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment