How to create pages in keystone.js from admin section

3.4k views Asked by At

i am new to node.js as well as keystone. i want to create new model called pages same as post which is default from keystone.

new model which i create is code as

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

pages.add({
    title: { type: String, required: true },
    state: { type: Types.Select, options: 'draft, published, archived', default: 'published', index: true },
    author: { type: Types.Relationship, ref: 'User', index: true },
    publishedDate: { type: Types.Date, index: true, dependsOn: { state: 'published' } },
    image: { type: Types.LocalFile, dest: 'public/uploads'},
    content: {
        brief: { type: Types.Html, wysiwyg: true, height: 150 },
        extended: { type: Types.Html, wysiwyg: true, height: 400 }
    },
});

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

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

then i created a route for pages

exports = module.exports = function(req, res) {

    var view = new keystone.View(req, res),
        locals = res.locals;

    // Init locals
    locals.section = 'pages';
    locals.data = {
        pages: []
    };

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

        var q = keystone.list('pages').paginate({
                page: req.query.page || 1,
                perPage: 10,
                maxPages: 10
            })
            .where('state', 'published')
            .sort('-publishedDate');
        q.exec(function(err, results) {
            locals.data.pages = results;
            next(err);
        });

    });

    // Render the view
    view.render('pages');
};

also created middleware

{ label: 'pages',       key: 'pages',       href: '/pages' }

this all i have done to create model....let me know what i am missing. when i call this from pages.jade file by follow code:

data.pages.title

this shows me nothing. Even not showing any error. Any help will be appreciated.

1

There are 1 answers

0
JME On BEST ANSWER

The results from the q.exec() callback is an array, which you assign to local.data.pages. Therefore data.pages in your Jade template is an array and data.pages.title would be null.

You'll need to iterate over the returned pages. The example below will render the page titles, assuming the returned array of results is not empty.

each page in data.pages
  p= page.title