16

Has anyone ever documented BackboneJS code with JSDoc?

I'm having problems annotating Backbone constructs such as:

User = Backbone.Model.extend({

    defaults: { a: 1 },

    initialize: function () {
        // ...
    },

    doSomething: function (p) {
        // ...
    }
});

Any advice appreciated. Thanks.

sean
  • 163
  • 1
  • 5
  • 1
    Because JSDoc is a port of JavaDoc. So it's not designed for JavaScript. Use [`docco`](http://jashkenas.github.com/docco/) – Raynos Jun 22 '11 at 11:11
  • @Raynos In general you're right, but JSDoc still have one huge benefit: it can give you autocomplete in an IDE. – Georgii Ivankin Jul 27 '11 at 11:31
  • @GeorgiyIvankin you show me an IDE that has fully working autocomplete without false information for complex javascript projects _and then_ I'll consider that a "benefit". I havn't found any such IDE – Raynos Jul 27 '11 at 12:16
  • @Raynos For example, in [JSDT](http://wiki.eclipse.org/index.php/ATF/JSDT) you can do something like this: http://www.jspaste.com/?id=305 Of course things are not as perfect as they are with say Java, but what is shown in this example already greatly simplifies working on big projects with lots of js in separate files. – Georgii Ivankin Jul 27 '11 at 21:23

2 Answers2

27

I think it works somehow like this, if you're talking about the JSDoc Toolkit:

User = Backbone.Model.extend(
/** @lends User.prototype */
 {
  /**
   * @class User class description
   *
   * @augments Backbone.Model
   * @constructs
   *
   * Text for the initialize method
   */
    initialize: function() {}
})

The important bit is the position of the @lends tag!

It can be a bit tricky, but if this doesn't work try out some of the other examples: http://code.google.com/p/jsdoc-toolkit/wiki/CookBook

chris_b
  • 1,294
  • 1
  • 12
  • 12
5

chris_b's answer helped me a lot, the sample as well as the link. I had to drop the @class annotation, though, or it would generate two entries for the class. Furthermore, I'm adding this answer to show how to annotate static class members (class level constants).

(We use require.js.)

define([
    'jquery', 'lodash', 'backbone'
], function($, _, Backbone) {
    "use strict";

    /**
     * Enumeration of constants that represent the different types of Hedgehogs.
     * @memberof models/Hedgehog
     * @enum {string}
     * @readonly
     */
    var types = { 'type1': 'Type 1', 'type2': 'Type 2' };

    var Hedgehog = Backbone.Model.extend(
    /** @lends models/Hedgehog.prototype */
    {
        /**
         * This is the model for Hedgehogs.
         *
         * @augments external:Backbone.Model
         * @constructs
         */
        initialize: function() {
            // your code
        },

        // some more methods
    }, {
        // static class members
        "types": types
    });
    return Hedgehog;
});
Risadinha
  • 16,058
  • 2
  • 88
  • 91