我和朋友正试图弄清楚教程中产生的代码到底发生了什么.我们担心客户端/服务器的流程是一次调用factory.js的第8行:
factory.js
app.factory('postFactory', ['$http', function($http)
{
var o = {
posts: []
};
o.upvote = function(post){
return $http.put('/posts/' + post._id + "/upvote").success(function(data){
post.upvotes += 1;
});
};
return o;
}]);
Run Code Online (Sandbox Code Playgroud)
MongoosePost.js
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
url: String,
upvotes: {type: Number, default: 0},
comments: [{type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
PostSchema.methods.upvote = function(cb)
{
this.upvotes += 1;
this.save(cb);
}
mongoose.model('Post', PostSchema);
Run Code Online (Sandbox Code Playgroud)
expressRouter.js
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose'); …Run Code Online (Sandbox Code Playgroud)