添加创建mixin并使用keystonejs

时间:2014-12-23 18:58:46

标签: javascript node.js keystonejs

我正在尝试为keystonejs创建一个jin mixin,这是我在index.jade上提出的内容

extends ../layouts/default
block bodycontent
    include ../mixins/focus.jade

和mixin玉文件

mixin focus(focus)
  section#focus.focus
    div.container
          div.service-icon= focus.image

这是javascript视图文件

var keystone = require('keystone'),
 Focus = keystone.list('Focus');

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

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

        // Set locals
        locals.section = 'focus';

        locals.data = {

        };

        view.on('init', function(next) {

                Focus.model.find()
                    .populate('author')
                    .sort('label')
                    .limit(4)
                    .exec(function(err, fields) {
                        // do something with posts
                        locals.data.fields = fields;
                        console.log(fields);
                        next(err);
                    });
        });

        view.render('focus');

};

当我渲染index.jade文件时,焦点部分缺失。我错过了什么?列表模型存在,因为我能够将其保存到数据库中。

1 个答案:

答案 0 :(得分:1)

与includes不同,你需要在声明后调用mixins。他们喜欢功能。

请参阅此处的文档:http://jade-lang.com/reference/mixins/

然后,假设locals中有一些数据定义了要聚焦的内容,您首先要包含定义mixin的文件,然后调用mixin并将数据传递给渲染。

我猜了一下,因为我无法看到你的整个项目结构,但这样的事情可能就是你所追求的:

路由处理程序

var keystone = require('keystone'),
    Focus = keystone.list('Focus');

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

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

    locals.section = 'focus';

    locals.data = {};

    view.on('init', function(next) {

            Focus.model.find()
                .populate('author')
                .sort('label')
                .limit(4)
                .exec(function(err, results) {
                    locals.data.results = results;
                    next(err);
                });
    });

    view.render('focus');

};

<强> MIXIN

mixin focus(focus)
    section#focus.focus
        div.container
            div.service-icon= focus.image

<强>模板

extends ../layouts/default
block bodycontent
    include ../mixins/focus.jade
    each i in data.results
        +focus(i)