把手嵌套每个声明

时间:2016-07-21 00:11:55

标签: handlebars.js

我正在尝试嵌套每个语句来处理我视图中可用的两个对象。 userIdcard的{​​{1}}与userId表上user的连接之间存在关系,这可能与此有关。我想我可能最终要注册一个助手来完成这项工作,但我不知道从哪里开始。我尝试使用以下句柄语法绕过此过程,但我得到TypeError: inverse is not a function

{{#each this.fullNameSlug ../user.fullNameSlug}}
            <h5>{{this.fullNameSlug}}</h5>
{{/each}}

以下是完整视图:

{{#each card}}
<div class="row">
    <div class="card col-md-6 col-md-offset-3">
        <div class="card-date">
            <p class="card-date">{{this.cardDateSlug}}</p>
        </div>
        <div class="card-header">
            <h3 class="card-title">{{this.title}}</h3>
            {{#each this.fullNameSlug ../user.fullNameSlug}}
            <h5>{{this.fullNameSlug}}</h5>
            {{/each}}
        </div>
        <div class="card-body">
{{/each}}

以下是视图中可访问的两个对象(卡片,用户)的路径:

/*====   /  ====*/

appRoutes.route('/') 

    .get(function(req, res){
        models.Card.findAll({
            order: 'annotationDate DESC',
            include: [{
                model: models.User,
                where: { organizationId: req.user.organizationId },
                attributes: ['organizationId', 'userId', 'fullNameSlug']
            }],
            limit: limitAmount.limit
        }).then(function(annotation){
            res.render('pages/app/activity-feed.hbs',{
                card: card,
                user: req.user
            });
        });
    })

卡片对象:

module.exports = function(sequelize, DataTypes) {

var path = require('path');
var moment = require('moment');

var Card = sequelize.define('card', {
    cardId: {
        type: DataTypes.INTEGER,
        field: 'card_id',
        autoIncrement: true,
        primaryKey: true
    },
    cardDate: {
        type: DataTypes.DATE,
        field: 'card_date',
        isDate: true
    },
    reportLink: {
        type: DataTypes.TEXT,
        field: 'report_link'
    },
    fileAttachment: {
        type: DataTypes.STRING,
        field: 'file_attachment'
    },
    userId: {
        type: DataTypes.INTEGER,
        field: 'user_id'
    },
    discoverySourceId: {
        type: DataTypes.INTEGER,
        field: 'discovery_source_id'
    }
},

 {
    freezeTableName: true,
    getterMethods: {
        cardDateSlug: function(){
            var date = new Date(this.getDataValue('annotationDate'));
            var momentDate = moment(date).utc().format("MM/DD/YYYY"); 

            return momentDate;

        }
    },
    classMethods: {
        associate: function(db) {
            Card.belongsTo(db.User, {foreignKey: 'user_id'}),
        }
    }
});
    return Card;
}

用户对象:

var bcrypt   = require('bcrypt-nodejs');

module.exports = function(sequelize, DataTypes) {

var User = sequelize.define('user', {
    userId: {
        type: DataTypes.INTEGER,
        field:'user_id',
        autoIncrement: true,
        primaryKey: true
    },
    firstName: {
        type: DataTypes.STRING,
        field: 'first_name'
    },
    lastName: {
        type: DataTypes.STRING,
        field: 'last_name'
    },
    email: {
        type: DataTypes.STRING,
        isEmail: true,
        unique: true,
        set: function(val) {
            this.setDataValue('email', val.toLowerCase());
        }
    },
    password: DataTypes.STRING,
    organizationId: {
        type: DataTypes.INTEGER,
        field: 'organization_id',
        allowNull: true
    },
    authenticationToken: {
        type: DataTypes.STRING,
        field: 'authentication_token'
    },
    resetPasswordToken: {
        type: DataTypes.STRING,
        field: 'reset_password_token'
    },
    resetPasswordExpires: {
        type: DataTypes.DATE,
        field: 'reset_password_expires'
    }
}, {
    freezeTableName: true,
    getterMethods: {
        fullNameSlug: function(){
            var fullName = this.getDataValue('firstName') + ' ' + this.getDataValue('lastName');
            return fullName;
        }
    },
    classMethods: {
        associate: function(db) {
            User.belongsToMany(db.Organization, { through: 'member', foreignKey: 'user_id'})
        },
        generateHash: function(password) {
            return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
        },
    },
    instanceMethods: {
        validPassword: function(password) {
            return bcrypt.compareSync(password, this.password);
        },
    },  
});
    return User;
}

0 个答案:

没有答案
相关问题