2017-03-12 40 views
0

我正在嘗試使Keystone進入CMS。所以,我需要文章,類別,ImagePage,AttachmentPage等模型。我提到的每個模型都有一些常用字段,如:標題,內容,元:{title,description,keywords}等等。如何重新排序Keystone的管理界面中的字段

在梯形校正模型構造是這樣的:

Article.add(fieldsCollectionObject) 

,所以我定義在外部文件中的公共字段:

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

module.exports = { 
    title: { type: T.Text, required: true }, 
    content: { type: T.Html, wysiwyg: true, height: 400 }, 
    meta: { 
     title: { type: T.Text }, 
     desc: { type: T.Textarea, height: 50 }, 
     keywords: { type: T.Text }, 
    }, 
    publishedDate: { type: T.Date, index: true, dependsOn: { state: 'published' } }, 
    state: { type: T.Select, options: 'draft, published, archived', default: 'draft', index: true }, 
}; 

,並在模型文件中有require'd它我做的:

const _ = require('lodash'); 
const pageDef = require('./common/Page.js'); 
const keystone = require('keystone'); 
const T = keystone.Field.Types; 

<...> 

Article.add(_.defaultsDeep({ 
    brief: { type: T.Html, wysiwyg: true, height: 150 }, 
    category: { type: T.Relationship, ref: 'Category', many: false, collapse: true }, 
    tags: { type: T.Relationship, ref: 'Tag', many: true }, 
}, defs.authored, pageDef)); 

現在,問題是在管理界面的字段順序 - 不出所料brief,categorytagspageDef開始。有什麼辦法可以施加我想要的訂單嗎?像titlebrief,content,<the rest>

回答

1

defaultsdefaultsDeep變異作爲參數傳遞給它的第一個對象(您的Keystone字段的初始對象)。要獲得自己的訂單,您需要按照您希望它們出現在對象中的順序將對象傳遞給_.defaultsDeep,從而將它們顯示在管理界面中的順序。

有用的是,重複項目不會包含在結果對象中。所以你會有這樣的事情:

const _ = require('lodash'); 
const pageDef = require('./common/Page.js'); 
const keystone = require('keystone'); 
const T = keystone.Field.Types; 

//.... 

let articleDef = { 
    brief: { type: T.Html, wysiwyg: true, height: 150 }, 
    category: { type: T.Relationship, ref: 'Category', many: false, collapse: true }, 
    tags: { type: T.Relationship, ref: 'Tag', many: true }; 
}; 

Article.add(_.defaultsDeep({ 
    title: pageDef.title, 
    brief: articleDef.brief, 
    content: pageDef.content}, 
pageDef, articleDef)); 
+0

就是這樣!我貼上了自己尋找像「排序:x」或類似的字段定義屬性,但你的解決方案更簡單但聰明。我將它擴展爲將實體頁面的分割部分與標題區分開來,當然還有功勞。:)感謝您的幫助! – Forseti

1

上面的答案竟然是要走的路。所以,我擴大,在它之上構建:

的lib/util.js中

const _ = require('lodash'); 

class Util { 
    static sourceFields (fields, ...sources) { 
     const source = _.defaultsDeep(...sources); 
     const result = []; 
     for (let fieldSet of fields) { 
      result.push(_.isArray(fieldSet) ? _.pick(source, fieldSet) : fieldSet); 
     } 
     return result; 
    } 
} 

module.exports = Util; 

模式/通用/ traits.js

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

module.exports = { 
    title: { type: T.Text, required: true }, 
    content: { type: T.Html, wysiwyg: true, height: 400 }, 
    indexImage: { type: T.CloudinaryImage }, 
    meta: { 
     title: { type: T.Text }, 
     desc: { type: T.Textarea, height: 50 }, 
     keywords: { type: T.Text }, 
    }, 
// <...> 
} 

型號/ Article.js

const util = require('../lib/utils.js'); 
const defs = require('./common/traits.js'); 
const keystone = require('keystone'); 
const T = keystone.Field.Types; 

// < var Article declaration... > 

const ownDef = { 
    brief: { type: T.Html, wysiwyg: true, height: 150 }, 
    category: { type: T.Relationship, ref: 'Category', many: false, collapse: true }, 
    tags: { type: T.Relationship, ref: 'Tag', many: true }, 
    images: { type: T.Relationship, ref: 'Image', many: true, collapse: true }, 
    attachments: { type: T.Relationship, ref: 'Attachment', many: true, collapse: true }, 
}; 

Article.add(...util.sourceFields([ 
    'Content', ['title', 'brief', 'content', 'indexImage', 'category', 'tags'], 
    'Media', ['images', 'attachments'], 
    'SEO', ['meta'], 
    'Status', ['pageType', 'author', 'state', 'publishedDate'], 
], ownDef, defs)); 

所以,在traits.js我定義公共領域,Article.js - 場我在文章模型只用。然後,在Article模型中,我使用sourceFields()函數的幫助將這些字段添加到列表中。 sourceFields()獲取一組字段集和未指定數量的字段定義對象(如ownDefdefs)。

字段集是字符串或字段名稱數組(定義對象中的鍵)。如果是字符串,它將成爲Admin UI中的頭部,如果它是數組,那麼它將是一組有序的字段,就像數組中的字段名一樣 - 該函數基本上將字段定義插入字段集中指定的「插槽」中。