填充相关的keystone列表

时间:2015-01-18 07:51:55

标签: node.js express mongoose keystonejs

所以我有一个Post模型(当然是内置的)。每个帖子都有一个作者。每个作者都有一个个人资料(我试图学习的时候是故意单独构建的。我在填写个人资料时遇到了实际问题

这是我在blog.js中的内容

    // Load the posts
view.on('init', function(next) {

    var q = keystone.list('Post').paginate({
            page: req.query.page || 1,
            perPage: 10,
            maxPages: 10
        })
        .where('state', 'published')
        .sort('-publishedDate')
        .populate('categories')
        .populate('author');

    if (locals.data.category) {
        q.where('categories').in([locals.data.category]);
    }

    q.exec(function(err, results) {
        locals.data.posts = results;

        async.each(results.results, function(post,_next) {
            post.author.populate('profile', function(err, author) {
                post.author = author;
                locals.data.posts.results.push(post);
                _next();
            });
        }, function(err) {
            next();
        })

    })
});

这是个人资料

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

var Profile = new keystone.List('Profile', {
    map: { name: 'nickname' },
    autokey: { path: 'slug', from: 'nickname', unique: true }
});

Profile.add({
    nickname: { type: String, required: true, initial: true},
    age: { type: Types.Number, initial: false, required: false, index: true },
    location: { type: Types.Text, initial: false, required: false, index: true }
});


Profile.defaultColumns = 'nickname, age, location';
Profile.register();

这是用户

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

/**
 * User Model
 * ==========
 */

var User = new keystone.List('User');

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
    password: { type: Types.Password, initial: true, required: true },
    profile: { type: Types.Relationship, ref: 'Profile', index: true },

    }, 'Permissions', {
    isAdmin: { type: Boolean, label: 'Can access Keystone', index: true }
});

// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function() {
    return this.isAdmin;
});


/**
 * Relationships
 */

User.relationship({ ref: 'Post', path: 'posts', refPath: 'author' });



/**
 * Registration
 */

User.defaultColumns = 'name, email, isAdmin';
User.register();

这是Post

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

/**
 * Post Model
 * ==========
 */

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

Post.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'draft', index: true },
    author: { type: Types.Relationship, ref: 'User', index: true },
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
    image: { type: Types.CloudinaryImage },
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    },
    categories: { type: Types.Relationship, ref: 'PostCategory', many: true }
});

Post.schema.virtual('content.full').get(function() {
    return this.content.extended || this.content.brief;
});

Post.defaultColumns = 'title, state|20%, author|20%, publishedDate|20%';
Post.register();

提前致谢!

0 个答案:

没有答案