简体   繁体   English

AngularJS服务中的JSDoc

[英]JSDoc within AngularJS services

I'm creating an AngularJS service to validate form inputs and I can't for the life of me figure out how to use JSDoc3 to create reasonable documentation. 我正在创建一个AngularJS服务来验证表单输入,我不知道如何使用JSDoc3来创建合理的文档。

Ideally I need to export the documentation for the validator service as well as the documentation for each validator (internal objects) 理想情况下,我需要导出验证器服务的文档以及每个验证器的文档(内部对象)

After googling around a bit I was able to get it kind of working by hacking around a bit with namespaces, but I'm wondering if there's a right way to do this. 在谷歌搜索了一下后,我能够通过使用命名空间来进行一些工作,但是我想知道是否有正确的方法来执行此操作。 A way that won't muck up somebody else's JSDoc if they include my service. 如果它们包含我的服务,那么这种方式不会破坏其他人的JSDoc。

Example: 例:

angular.module('services.utility')
.factory('validator', [function () {
    var validators = {
        /**
         * Requires a field to have something in it.
         * @param  {String|Boolean} val    The value to be checked.
         * @return {Object}
         */
        'required': function(val){
            // check against validator
            return {'valid': true, 'msg': 'passed!'};
        },
        /**
         * Requires a field to be less than a number.
         * @param  {Number} val    The value to be checked.
         * @param  {Number} check  The value to be checked against.
         * @return {Object}
         */
        'lessThan': function(val){
            // check against validator
            return {'valid': true, 'msg': 'passed!'};
        }
    };
    return {
        /**
         * This is the main validation routine. 
         * @param  {Object} vObjs An object to be processed.
         * @return {Boolean}
         */
        'validate': function(thingsToValidate){
            // run some validations from the validators
                    // and return a bool.
        }
    };
}]);

In a perfect world, modifications to the above would let me generate a nice, non-global, JSDoc hierarchy where a user could read about how to use the validator as a whole, or dive in and look what needed to be passed in for each type of validation. 在一个完美的世界中,对上述内容的修改将让我生成一个漂亮的,非全局的JSDoc层次结构,用户可以在其中阅读有关如何使用验证器作为一个整体,或潜入并查看需要传递给每个验证器的内容。验证类型。

Thanks so much for any help you can give! 非常感谢您提供的任何帮助!

The way my team works is that we actually only document the actual factory function as if the user were going to read it. 我的团队工作方式是我们实际上只记录实际的工厂功能,就像用户要阅读它一样。 You've actually skipped over that guy. 你实际上已经跳过那个人了。 Either way, you can treat that as the entry point for your documentation and combine it with your "method" documentation for the full picture. 无论哪种方式,您都可以将其视为文档的入口点,并将其与“方法”文档结合使用,以获得完整的图片。 You could make use of the @namespace documentation in combination with this. 您可以结合使用@namespace文档。

/** Here is how you use my factory. @param @return */
my.validator.factory = function() { return { validate: function(){} }; }
/** Here are some validators. @enum {Function(*):ValidationResult} **/
my.validator.Validators = {}

module.factory('validator', my.validator.factory);

Depending on what you really want, you may prefer to use a prototype. 根据你真正想要的,你可能更愿意使用一个原型。 That's where documentation can really shine: 这就是文档可以真正发挥作用的地方:

/** Here is how you use my factory. @param @return @constructor */
my.Validator = function() {}
/** My validation function. @param @return */
my.Validator.prototype.validate = function() {}
/** Here are some validators. @enum {Function(*):ValidationResult} **/
my.Validator.Validators = {}

module.service('validator', my.Validator);

Using the prototype with your documentation really sticks it all together for me. 将原型与您的文档一起使用对我来说非常重要。 It's like documenting your validator as one class entity, which makes the most sense to me. 这就像将您的验证器记录为一个类实体,这对我来说最有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM