jashkenas/coffee-script — Issue #1883: Extended/Included/Initialized Callback?
Author: viatropos
Created: 2011-11-20 08:56:08+00:00
State: open

I saw this issue was closed:

https://github.com/jashkenas/coffee-script/issues/867

Having something like that would solve the following edge case:

When you have `ParentClass` and `ChildClass extends ParentClass`, and you have a "class attribute" on `ParentClass`, changing the value of that class attribute on either `ParentClass` or `ChildClass` will effect both. This is only for arrays and objects it seems.

I see how that's expected behavior, but I want to be able to **initialize a class variable for each sub class**. How do I do that

Here's the example showing what currently happens:

``` coffeescript

specialProperties = ['included', 'extended', 'prototype']

class Base
@extend: (object) ->
for key, value of object when key not in specialProperties
@[key] = value
@

SomeModule =
overriddenClassProperty: ["I'll be manually overridden in subclass"]
nonOverriddenClassProperty: ["I won't be overridden!"]

class User extends Base
@extend SomeModule

class Admin extends User
@overriddenClassProperty: ["been overridden"]

User.nonOverriddenClassProperty.push "I'm the same..."
Admin.nonOverriddenClassProperty.push "Still the same..."

alert User.name + ": " + User.overriddenClassProperty.join(" ")
# User: I'll be manually overridden in subclass
alert Admin.name + ": " + Admin.overriddenClassProperty.join(" ")
# Admin: been overridden
alert User.name + ": " + User.nonOverriddenClassProperty.join(" ")
# User: I won't be overridden! I'm the same... Still the same...
alert Admin.name + ": " + Admin.nonOverriddenClassProperty.join(" ")
# Admin: I won't be overridden! I'm the same... Still the same…
```

Here's what I'd like to be able to do:

``` coffeescript

specialProperties = ['included', 'extended', 'prototype']

class Base
@extend: (object) ->
for key, value of object when key not in specialProperties
@[key] = value

if extended = object.extended
@extendedCallbacks.push extended
extended.apply(@)

@

# something like this built in...
@extended: ->
for callback in @extendedCallbacks
callback.apply(@)

SomeModule =
overriddenClassProperty: ["I'll be manually overridden in subclass"]

extended: ->
@extendedOverriddenClassProperty = ["Awesome! I was overridden!"]

class User extends Base
@extend SomeModule

class Admin extends User
@overriddenClassProperty: ["been overridden"]

User.extendedOverriddenClassProperty.push "I'm for the User..."
Admin.extendedOverriddenClassProperty.push "I'm for the Admin..."

alert User.name + ": " + User.overriddenClassProperty.join(" ")
# User: I'll be manually overridden in subclass
alert Admin.name + ": " + Admin.overriddenClassProperty.join(" ")
# Admin: been overridden
alert User.name + ": " + User.extendedOverriddenClassProperty.join(" ")
# User: Awesome! I was overridden! I'm for the User
alert Admin.name + ": " + Admin.extendedOverriddenClassProperty.join(" ")
# Admin: Awesome! I was overridden! I'm for the Admin
```

Does this exist, or is this something that could be added?