【问题标题】:How to add virtual property of type Array using Keystone.js?如何使用 Keystone.js 添加 Array 类型的虚拟属性?
【发布时间】:2014-03-30 14:36:56
【问题描述】:

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

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)

【问题讨论】:

    标签: javascript content-management-system keystonejs


    【解决方案1】:

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

    我猜你的意思是:

    两者都可以通过直接修改猫鼬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 的列表,这意味着这些字段不会显示在管理 UI 中。 an issue 可以在管理 UI 中添加对自定义模板的支持,但将来会允许这样做。

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

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

    【讨论】:

    • 我尝试将令牌属性直接添加到架构中,这很有效。但是当我创建一个新的 Info 项时,它会在 tokens 属性中存储一个空数组。我只是希望这个属性在我的 API 响应期间存在。似乎在发送响应时动态添加的属性会被删除(使用 res.send(items);)我不想在 getter 内进行计算,因为当它发生时我几乎无法控制。
    猜你喜欢
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多