如何使用Keystone.js添加Array类型的虚拟属性



这是我的模型代码:"Info"及其产生问题的tokens属性。

var keystone = require('keystone'),
Types = keystone.Field.Types;
var Info = new keystone.List('Info');
Info.add({
title: { type: String, required: true, initial: true },
subtitle: { type: String, initial: true },
content: { type: Types.Markdown, height: 500, initial: true },
author: { type: Types.Relationship, ref: 'User', initial: true },
date: { type: Types.Date, default: Date.now, initial: true },
published: { type: Boolean, default: false, initial: true },
tokens: { type: Array, virtual: true, noedit: true, collapse: true }
});
Info.defaultColumns = 'title, author, date|15%, published|15%'
Info.register();

当运行应用程序时,我得到:

Error: Unrecognised field constructor: function Array() { [native code] }
at List.field (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:315:10)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:200:16)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:191:5)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:230:5)
at Function._.each._.forEach (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/node_modules/underscore/underscore.js:82:22)
at List.add (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:204:4)
at Object.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/models/infos.js:4:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)

我不确定你打算用代币存储/做什么,所以如果这不能回答你的问题,请澄清:)

我猜你的意思是:

  • 存储在数据库中的数组路径,或者
  • 一种虚拟属性,其值不存储,而是在运行时计算的

两者都可以通过直接修改猫鼬schema而不是在List上使用Keystone的add方法来实现。

要添加数组路径(例如,您可以在保存时存储通过某个过程生成的字符串标记数组),您可以这样做:

var keystone = require('keystone'),
Types = keystone.Field.Types;
var Info = new keystone.List('Info');
Info.add({
title: { type: String, required: true, initial: true },
subtitle: { type: String, initial: true },
content: { type: Types.Markdown, height: 500, initial: true },
author: { type: Types.Relationship, ref: 'User', initial: true },
date: { type: Types.Date, default: Date.now, initial: true },
published: { type: Boolean, default: false, initial: true }
});
Info.schema.add({
tokens: { type: [String] }
});
Info.defaultColumns = 'title, author, date|15%, published|15%';
Info.register();

要创建一个虚拟属性,您需要使用如下getter来指定它:

Info.schema.virtual('tokens', function() {
var tokens = [];
// calculate tokens somehow
return tokens;
});

通过访问模式,您可以绕过Keystone的列表,这意味着字段不会显示在Admin UI中。在Admin UI中添加对自定义模板的支持存在问题,但这将在未来允许这样做。

数组字段类型也有一个问题,因此,如果您目前正在数组中存储字符串,那么在实现该功能时,您可以将其包含在Admin UI中。

与此相关的是,mongoose提供的所有功能都可以通过模式获得,因此您也可以定义自定义方法、静态和保存前/保存后挂钩。有关如何使用猫鼬模式的更多信息,请查看指南。

最新更新