Skip to content

Instantly share code, notes, and snippets.

@lancejpollard
Created March 19, 2012 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancejpollard/2127034 to your computer and use it in GitHub Desktop.
Save lancejpollard/2127034 to your computer and use it in GitHub Desktop.
CoffeeScript + Ember Tests

Given:

class Base

class User extends Base
  @name: true

Coffeescript generates a constructor for you like this:

Base = (function() {

  function Base() {}

  return Base;

})();

User = (function(_super) {

  __extends(User, _super);

  function User() {
    User.__super__.constructor.apply(this, arguments);
  }

  User.name = true;

  return User;

})(Base);

In order to make this use Ember.js, you have to change both the __extends and that function User call, to look like this:

User = (function(_super) {
  var User;
  
  if (_super.isEmberObject)
    User = _super.extend();
  else {
    User = function() {
      User.__super__.constructor.apply(this, arguments);
    }
  }

  User.name = true;

  return User;

})(Base);

But then still, that won't work… You need to apply the class/instance methods the same way ember does:

User = (function(_super) {
  var User;
  
  if (_super.isEmberObject) {
    User = _super.extend();
    
    User.reopenClass({
      name: true
    });
    
  } else {
    User = function() {
      User.__super__.constructor.apply(this, arguments);
    }
    
    User.name = true;
  }

  return User;

})(Base);

I tried doing _.extend(User, _super.extend()), but that doesn't apply the instance/class methods properly. There's no way to make the constructor function work differently from the outside either…

Hmm… Looks like ember would have to change their architecture to make this work anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment