Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document a factory that returns a class in angular with ngdoc?

Given an angular app with a factory that returns a class, as such:

angular.module('fooApp').factory('User', function(){
    function User(name){
        this.name = name;
    }

    User.prototype.greet = function(){
        return "Howdy, " + this.name;
    }

    return User;
});

Using ngdoc (the special flavor of jsdoc angular uses), how do I document the initializer without defining it as a method?

Right now, this is what I've tried:

/**
 * @ngdoc service
 * @name fooApp.User
 * @description User factory.
 */

angular.module('fooApp').factory('User', function(){

    /**
     * @ngdoc method
     * @methodOf fooApp.User
     * @name Initializer
     * @description Initializes a new User object with a name
     */
    function User(name){
        this.name = name;
    }

    User.prototype.greet = function(){
        return "Howdy, " + this.name;
    }

    return User;
});

But this results in the User initializer (the User function that accepts the parameter name) being treated like a method with the name Initializer, which is confusing to people trying to use this code.

I've tried adding the @constructor flag, but this has no effect of the html dgeni ends up generating.

Thank you.


UPDATE: Removed references to dgeni. I was under the impression that the plugin I was using (grunt-ngdocs) uses dgeni behind the scenes, but this is not the case.

like image 801
Yuval Karmi Avatar asked Apr 04 '15 16:04

Yuval Karmi


1 Answers

This is how I would do it not having any experience with Angular in particular:

/**
 * @ngdoc service
 * @name fooApp.User
 * @description User factory.
 */

angular.module('fooApp').factory('User', function(){

    /**
     * @ngdoc method
     * @constructs fooApp.User
     * @description Initializes a new User object with a name
     */
    function User(name){
        this.name = name;
    }

    User.prototype.greet = function(){
        return "Howdy, " + this.name;
    }

    return User;
});
like image 56
SGD Avatar answered Sep 25 '22 14:09

SGD