您好我正在尝试MEANjs进行开发。
我为“帐户”,“收入”和“费用”制作了一个包含CRUD的应用程序。其中每个都是使用yo meanjs:crud-module <module-name>
创建的。现在我不会扩展我的项目类型:“转移”,包括收入和费用,即创建转移创建发送帐户的费用和接收帐户的收入。
我想尽可能轻松而合乎逻辑地创建它。
我想尽可能将服务器端的大部分逻辑放在合理的位置。因此,前端通过组合收入和费用模型中的数据将传输标识为在后端控制器中创建的传输对象。
为了确定收入实际上属于转移,我将以下属性添加到模型中,我使用属性来判断它是否是转移:isTransfer,以及使用“费用”属性所属的费用。 / p>
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Income Schema
*/
var IncomeSchema = new Schema({
name: {
type: String,
default: '',
required: 'Please fill Income name',
trim: true
},
amount: {
type: String,
default: '',
required: 'Please fill Income amount',
trim: true
},
date: {
type: Date,
default: '',
required: 'Please fill Income date',
trim: true
},
monthly: {
type: Boolean,
default: '',
required: 'Please fill whether income is recurring monthly',
trim: true
},
yearly: {
type: Boolean,
default: '',
required: 'Please fill whether income is recurring yearly',
trim: true
},
account: {
type: Schema.ObjectId,
ref: 'Account',
required: 'Please select account'
},
isTransfer: {
type: Boolean,
default: false,
trim: true
},
expense:{
type: Schema.ObjectId,
ref:'Expense'
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Income', IncomeSchema);
然后我想在整个堆栈中创建传输就像运行一样简单:
yo meanjs:crud-module transfers
我的下一步是删除后端传输模型并重写后端控制器,以便每次尝试创建传输时都会创建收入和费用。
然后,控制器还会从后端控制器一直返回一个实际的“传输对象”,直到前端。
最初,我尝试将transfer对象添加为仅存在于前端,并发送请求以从前端服务一直创建收入和费用但是它堵塞了很多代码并且我不得不对后端执行多次调用创建一个转移。即
我认为在后端控制器中创建传输对象会更有意义。 这是管理转移对象的合理方式吗?
以另一种方式创建转移更有意义吗? 或者在堆栈的另一个层面?
我想我可以创建一个转移模型,其中只包含对收入和支出的引用,但我仍然需要创建收入/支出对象:
Sooo,我运行了yo meanjs:crud-module <module-name>
命令并生成了一个传输模块,然后删除了整个传输模型,并从服务器控制器中注释掉了它。我保留了前端表示,列表和创建,编辑等服务器控制器需要重新设计,以便它与收入和费用模型相关。示例如下:
服务器转移控制器的创建方法因此成为收入和费用的憎恶,如soo:
/**
* Create a Income and Expense
*/
exports.create = function(req, res) {
var income = new Income(req.body);
var expense = new Expense(req.body);
expense.user = income.user = req.user;
console.log('Request: ' + JSON.stringify(req.body));
//This should create an income and expense that is part of a transfer
expense.isTransfer = income.isTransfer = true;
//Generate ID's manually to create the relation between income and expense:
income.expense = expense._id = mongoose.Types.ObjectId();
expense.income = income._id = mongoose.Types.ObjectId();
//Set income and expense receiving and sending account, these attributes where simply attached to the body:
income.account = req.body.receivingAccount;
expense.account = req.body.sendingAccount;
//Save income if successfull, also save expense, if both succeeds return income and expense
income.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
expense.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp({income: income, expense: expense});
}
});
}
});
};
创建Transfer的客户端控制器,不是那么难看,请注意创建的Transfer对象与mongoose模型中定义的传输对象不对应:
// Create new Transfer when received in the server controller this yields both an income and an expense.
$scope.create = function() {
// Create new Transfer object, this defines what your request.body will contain.
var transfer = new Transfers ({
name: this.name,
amount: this.amount,
date: this.date,
monthly: this.recurring==='monthly',
yearly: this.recurring==='yearly',
receivingAccount: this.receivingAccount._id,
sendingAccount: this.sendingAccount._id
});
// Redirect after save
transfer.$save(function(response) {
$location.path('transfers/' + response._id);
// Clear form fields
$scope.name = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
由于