Skip to content

Shorthand the creation of reusable properties. #67

@tbranyen

Description

@tbranyen

I've noticed in much of the documentation and examples the common definition of reusable properties, such as: width, height, tickFormat, etc. These functions are bulky and not particularly dry in my opinion.

The following proof of concept illustrates the concept:

d3.chart("SomeChart", {
  initialize: function(options) {
    // Configurable properties.
    this.prop("width");
    this.prop("height");

    // Configure dimensions.
    this.width(960);
    this.height(120);
  },

  // Create re-usable methods that act as getters/setters.
  prop: function(name, callback) {
    this[name] = function(val) {
      if (arguments.length === 0) {
        return this["_" + name];
      }

      // Reassign the value.
      this["_" + name] = val;

      if (callback) {
        callback.call(this, val);
      }

      return this;
    };
  }
});

Is this a common enough task to warrant a convenience method within d3.chart itself? I think it could easily cut down on a significant amount of common repeated methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions