1

我正在扩展 keystone.js 以支持模型中的多种语言。

原始模型如下所示:

Post.add({
    title: { type: String, required: true },
    publishedDate: { type: Types.Date, index: true },
    //... more irrelevant fields
});

我的扩展模型如下所示:

Post.add({
    title: { type: String, required: true },
    en: {
        title: { type: String },
        publishedDate: { type: Types.Date, index: true },
        //... more irrelevant fields
    },
    es: {
        title: { type: String },
        publishedDate: { type: Types.Date, index: true },
        //... more irrelevant fields
    },
    //... more languages following the same pattern
});

我在视图模板中使用嵌套的 publishedDate 属性时遇到问题。

适用于原始模型的原始视图代码:

{% if post.publishedDate %}
    <br>on {{ post._.publishedDate.format('MMMM DD, YYYY') }}
{% endif %}

为我的新模型调整了视图代码(这是我遇到问题的地方):

{% if post[lang].publishedDate %}
    <br>on {{ post[lang]._.publishedDate.format('MMMM DD, YYYY') }}
{% endif %}

使用post[lang].xxxxxxI 可以参考我帖子中的所有其他项目(例如post[lang].title.

我是否遗漏了导致这些嵌套下划线函数失败并出现以下错误的内容?

non_object_property_load 请求引发错误”/en/blog
TypeError:无法读取未定义的属性“publishedDate”



编辑:
它变得更加奇特......下面似乎有效:

{% if post[lang].publishedDate %}
    <br>on {{ post._[lang].publishedDate.format('MMMM DD, YYYY') }}
{% endif %}

如果有人可以解释为什么这样做(或更好的方法来实现这一点),我将不胜感激。

4

1 回答 1

1

@JRulle,我认为错误在以下行:

<br>on {{ post[lang]._.publishedDate.format('MMMM DD, YYYY') }}

问题是下划线方法属于List对象。例如,要为您访问下划线方法,post.en.publishedDate请使用以下语法:post._.en.publishedDate.format('MMMM DD, YYYY')

您应该将上面的行更改为:

<br>on {{ post._[lang].publishedDate.format('MMMM DD, YYYY') }} 
于 2015-03-30T16:32:39.220 回答