【问题标题】:Cannot format nested date field in keystone.js无法格式化 keystone.js 中的嵌套日期字段
【发布时间】:2015-03-31 00:32:39
【问题描述】:

我正在扩展 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].xxxxxx,我可以引用我帖子中的所有其他项目(例如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 %}

如果有人能解释为什么这样做(或更好的方法),我将不胜感激。

【问题讨论】:

    标签: javascript underscore.js momentjs swig-template keystonejs


    【解决方案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') }} 
    

    【讨论】:

    • 这是有效的。感谢您的解释,因为这让我发疯了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2012-11-05
    • 1970-01-01
    • 2015-09-13
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多