jaredhanson / electrolyte

Elegant dependency injection for Node.js.
MIT License
564 stars 61 forks source link

Extending the container. #3

Closed bweston92 closed 7 years ago

bweston92 commented 10 years ago

How would I go about getting the following to work. At the minute @create doesn't exist even though I'm extending the container instance. Any ideas?

Container = require 'electrolyte'
path = require 'path'

exports = module.exports = class App extends Container

    # @var object
    paths: {}

    # @var Configuration
    config: null

    constructor: ->
        @bootPaths()
        @bootConfig()
        @bootExpress()
        @bootServices()

    bootPaths: =>
        @paths =
            views: path.join __dirname, 'views'
            src: path.join __dirname, 'src'
            public: path.join __dirname, 'public'

    bootConfig: =>
        @config = @create 'libs/config'
dpatti commented 10 years ago

This is because Container's prototype has a constructor method, which in JavaScript is usually the function that the prototype is attached to:

> x = new Function()
[Function]
> x.prototype.constructor == x
true

CoffeeScript expects all classes to behave this way, so to extend, you have to do it the old way:

Container = require('electrolyte').Container

class App
  # Set up the new prototype object
  @prototype = Object.create(Container.prototype)

  constructor: ->
    # Call super
    Container.call(this)