我可以通过以下方式实现多键映射产生的价值 http://keystonejs.com/docs/database/#fields-watching:
示例模型Post可以写成如下:
var keystone = require('keystone'),
Types = keystone.Field.Types;
var Post = new keystone.List('Post', {
autokey: { path: 'slug', from: 'newKey', unique: true },
map: { name: 'newKey' },
defaultSort: '-createdAt'
});
Post.add({
newKey: { type: Types.Text, watch: true, noedit: true, value: function (callback) {
var author = this.author;
var title = this.title;
keystone.list('User').model.findOne({_id: this.author.toString()}).exec(function (err, u) {
if (err) {
callback(err, "unknown")
} else {
var r = "("+u.name.first+" "+u.name.last+")-"+title;
callback(null, r)
}
});
}},
title: { type: String, required: true, index: true, initial: true },
state: { type: Types.Select, options: 'draft, published, archived', default: 'draft' },
author: { type: Types.Relationship, ref: 'User', index: true, initial: true },
createdAt: { type: Date, default: Date.now },
publishedAt: Date,
image: { type: Types.CloudinaryImage },
content: {
brief: { type: Types.Html, wysiwyg: true, height: 150 },
extended: { type: Types.Html, wysiwyg: true, height: 400 }
}
});
Post.defaultColumns = 'title, state|20%, author, publishedAt|15%'
Post.register();
对于上述模型,在创建 Post 时,首先会询问 Title 和 Author:
这将创建一个像这样的帖子,其中包含多个字段作为地图: