3

Want to know how to specify multiple fields in Keystone.js List Map.

For example, based on the Keystone Data Model documentation:

http://keystonejs.com/docs/database/

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

var Post = new keystone.List('Post', {
    autokey: { path: 'slug', from: 'title', unique: true },
    map: { name: 'title' },
    defaultSort: '-createdAt'
});

Post.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft' },
    author: { type: Types.Relationship, ref: 'User' },
    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();

Can I make use of both title and author to create a map, such that both 2 fields will be used in foreign Model Relationship?

var Post = new keystone.List('Post', {
    autokey: { path: 'slug', from: 'title', unique: true },
    map: { name: 'author, title' },
    defaultSort: '-createdAt'
});

I got an "undefined" in Admin UI.

Thomas Choy
  • 321
  • 2
  • 13

1 Answers1

2

I can achieve the multiple key map by Genereated Value:

The example model Post can be written as below:

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

For the above model, when creating Post, it will initially ask for Title and Author:

enter image description here

This will create a Post like this, with mutliple fields as a map:

enter image description here

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Thomas Choy
  • 321
  • 2
  • 13