如何添加类型Array的虚拟属性使用Keystone.js? [英] How to add virtual property of type Array using Keystone.js?

查看:490
本文介绍了如何添加类型Array的虚拟属性使用Keystone.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型的代码:'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},
:{type:Types.Markdown,height:500,initial:true},
author:{type:Types.Relationship,ref:'User',initial:true},
date:{type:类型:数组,默认值:Date.now,initial:true},
已发布:{type:Boolean,default:false,initial:true},
tokens: noedit:true,collapse:true}
});

Info.defaultColumns ='title,author,date | 15%,published | 15%'
Info.register

运行应用程序时:

 错误:无法识别的字段构造函数: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)
在列表。< anonymous> (/ Users / bobmoff / Dropbox(Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:200:16)
在列表。< anonymous> (/ Users / bobmoff / Dropbox(Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:191:5)
在列表。< anonymous> (/ Users / bobmoff / Dropbox(Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:230:5)
在函数._。each._。 (/ Users / bobmoff / Dropbox(Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/node_modules/underscore/underscore.js:82:22)
在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)
在Module._compile(module.js:456:26)
at Module.load(module.js:356:32)


解决方案

我不知道你打算存储/使用令牌,所以如果这不是回答您的问题,请澄清:)



我猜你要么意味着:





两者都可以通过直接修改mongoose schema 而不是使用Keystone的 add 方法。



要添加一个数组路径,存储通过一些进程生成的字符串数组数组保存)你可以这样做:

  var keystone = require '),
Types = keystone.Field.Types;

var Info = new keystone.List('Info');
Info.add({
title:{type:String,required:true,initial:true},
subtitle:{type:String,initial:true},
:{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 = [];
//计算令牌某种方式
返回令牌;
});

通过访问模式,您忽略了Keystone的列表,这意味着字段不会显示在管理UI。有问题可以在管理界面中添加对自定义模板的支持,以便将来允许这样做。



阵列字段类型也有问题,因此如果您现在将字符串存储在数组中,则可以在实施该功能时将其包含在管理界面中。



在相关说明中,功能mongoose提供可以通过模式,所以你可以定义的东西,如自定义方法,静态和前/后保存挂钩。有关可以使用mongoose模式进行操作的详情,请参阅指南


This is my code for my model: 'Info' and its the tokens property that creates the problem.

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();

When running the app i get:

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)

解决方案

I'm not sure what you are planning to store / do with tokens, so if this doesn't answer your question please clarify :)

I'm guessing you either mean:

Both are possible by modifying the mongoose schema directly, rather than using Keystone's add method on the List.

To add an array path (so you could, for example, store an array of string tokens generated through some process on save) you would do this:

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();

To create a virtual property, you'd specify it with a getter like this:

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

By accessing the schema you bypass Keystone's list, which means the fields won't show up in the Admin UI. There's an issue to add support for custom templates in the Admin UI though which will allow this in the future.

There's also an issue for an array field type, so if you are storing strings in an array for now you'll be able to include it in the Admin UI when that feature is implemented.

On a related note, all the functionality mongoose offers is available via the schema so you can define things like custom methods, statics and pre / post save hooks too. For more about what you can do with mongoose schemas, check out the guide.

这篇关于如何添加类型Array的虚拟属性使用Keystone.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆