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

标签 javascript content-management-system keystonejs

这是我的模型代码:“信息”及其产生问题的 token 属性。

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)

最佳答案

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

我猜你的意思是:

两者都可以通过直接修改 mongoose 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 的列表,这意味着这些字段不会显示在管理界面中。有 an issue在 Admin UI 中添加对自定义模板的支持,这将在未来实现。

array field type 也有问题,因此如果您现在将字符串存储在数组中,您将能够在实现该功能时将其包含在管理 UI 中。

在相关说明中, Mongoose 提供的所有功能都可以通过模式使用,因此您也可以定义自定义方法、静态和保存前/后 Hook 等内容。有关您可以使用 Mongoose 模式做什么的更多信息,请查看 guide .

关于javascript - 如何使用 Keystone.js 添加 Array 类型的虚拟属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063884/

相关文章:

php - Joomla 3 : Hide Class on Home Page

javascript - Node Keystonejs 意外 token 导出/导入

node.js - 在 Digital Ocean 上部署新的 node.js keystoneJS 应用程序

javascript - 快速点击会产生太多请求

javascript - zserge/lorca ui.Eval 没有返回值

javascript - 正则表达式:嵌套标签

ruby-on-rails - Refinery CMS 查询 - 呈现 :body content 时 content_for 的 NoMethodError

content-management-system - 如果用户在管理部分没有权限,如何从菜单中隐藏项目?

Keystonejs 4.0 文件系统存储适配器图片预览

javascript - 单个 where() 条件导致查询不返回任何内容