2014-02-27 137 views
8

這是我的模型代碼:'Info'及其創建問題的令牌屬性。如何使用Keystone.js添加Array類型的虛擬屬性?

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) 

回答

8

我不知道您打算存儲/做標記,因此,如果這不回答你的問題,請澄清一下:)

我猜你要麼意味着:

兩者通過修改貓鼬schema直接,而不是使用梯形的add方法對List是可能的。

要添加陣列路徑(這樣你就可以,例如,存儲通過上節省一些過程中產生的字符串標記的數組),你可以這樣做:

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

要創建一個虛擬的財產,你」 d用這樣一個getter指定:

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

通過訪問您繞過梯形的列表,這意味着該領域的管理用戶界面不會出現的模式。有an issue可以在管理界面中添加對自定義模板的支持,但將來會允許這樣做。

array field type也存在問題,因此如果您現在將字符串存儲在數組中,那麼當該功能實施時,您可以將其包含在管理界面中。

在相關說明中,mongoose所提供的所有功能均可通過模式獲得,因此您可以定義諸如自定義方法,靜態和前/後保存掛鉤等功能。有關可以用貓鼬模式做什麼的更多信息,請查看guide

+3

我試着直接向模式添加令牌標籤,並且工作正常。但是,當我創建一個新的Info項目時,它會在tokens屬性中存儲一個空數組。我只是希望這個屬性在我的API響應中存在。看起來在發送響應時使用動態添加的屬性會被刪除(使用res.send(items);)我不想在getter中進行計算,因爲當它發生時,我幾乎不能控制它。 – bobmoff