I am trying to add a hidden fieldtype called 'admin' which just has the user id of the person creating the model item. This works fine locally but for some reason doesn't work on a server. Here is what I did, maybe it is because I modified a file inside keystone/lib?
/Models/Group.js
var keystone = require('keystone'),
Types = keystone.Field.Types;
/**
* Group Collection Model
* =============
*/
var Group = new keystone.List('Group');
Group.add({
name: { type: String, required: true, initial: true },
createdAt: { type: Date, default: Date.now },
groupId: { type: Types.Admin, required: true, initial: true, default: 'placeholder' }
});
Group.defaultColumns = 'name';
Group.register();
/node_modules/keystone/lib/fieldTypes I added admin.js
/*!
* Module dependencies.
*/
var util = require('util'),
utils = require('keystone-utils'),
super_ = require('../field');
/**
* Text FieldType Constructor
* @extends Field
* @api public
*/
function admin(list, path, options) {
this._nativeType = String;
this._underscoreMethods = ['crop'];
admin.super_.call(this, list, path, options);
}
/*!
* Inherit from Field
*/
util.inherits(admin, super_);
/**
* Crops the string to the specifed length.
*
* @api public
*/
admin.prototype.crop = function(item, length, append, preserveWords) {
return utils.cropString(item.get(this.path), length, append, preserveWords);
};
/*!
* Export class
*/
exports = module.exports = admin;
/node_modules/keystone/lib/fieldTypes/index.js
added this:
exports.Admin = require('./admin');
/node_modules/keystone/templates/fields/admin
added a form.jade and initial.jade template
When you say "on a server", I will assume you mean on some Node.js hosting provider (i.e. Heroku, OpenShift, etc.). If that's the case and it's working locally, as you mentioned, I suspect that you may not be committing the node_modules
folder in Git and therefore the changes you implemented are not being uploaded to your provider along with the rest of your code. Check your .gitignore
file to see of node_modules
is listed there. If it is, remove node_modules
from he file, commit and push to your provider, and your app should work on your provider the same way it does locally.
That said, I'm currently working on a pull request (#490) to KeystoneJS that will optionally add and automatically update createdBy
, createdAt
, modifiedBy
, and modifiedAt
to a model. We are currently discussing the best way to go about implementing it, but I suspect my PR, or a modified version of it, will soon be pulled and available in the near future.
来源:https://stackoverflow.com/questions/25151588/add-custom-fieldtype-in-keystone-js