常量 'in' 值作为 REST API 值放心

Constant 'in' values as REST API value at ease

在此 SailsJS 模型中,我们将静态选项值保留在 'in' 键属性中。

status: {
    type : 'string',
    defaultsTo : 'Active',
    in : ['Active', 'InActive']
}

这些值在 REST 中是否可用 API -> 最好是 /OPTIONS 格式?

愿意为这些不变的价值观提供一个真理来源。 想要了解如何通过前端应用程序的 REST API 发送这些值的最佳实践。当前端应用程序和第三方 API 用户都可以获得相同的真实来源时,那么维护应用程序就非常容易了。请提出建议。

感谢和问候,拉吉

恐怕你必须制作一个 OptionController 并手动完成。对于这些静态选项,您可以将它们写入配置文件,然后使用 sails.config.xxx 访问它们。例如: 在 config/whatever.js

module.exports={
    activeChoices:['active','inactive','donotknow','xxx']
}

创建OptionController.js,并设置路由

Route.js

'GET /statusOptions/ :{
    controller:'OptionController',
    action:'statusOptions'
}

然后在你的 api/Controller/OptionController.js

module.exports={
    statusOptions:function(req,res){
        return res.json(sails.config.activeChoices);
    }
}

在你的模型中

status:{type:'string',defaultsTo:'Active',in:sails.config.activeChoices}