Skip to content

Commit

Permalink
Add test HTML; define module either via module.exports or on window
Browse files Browse the repository at this point in the history
  • Loading branch information
minglecm committed Sep 4, 2013
1 parent 064da0f commit 2d193af
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "coffeescript-mixins",
"version": "0.0.3",
"version": "0.0.4",
"author": "Caleb Mingle <me@caleb.io>",
"description": "easy to use mixins with CoffeeScript",
"main": "lib/mixins.js",
Expand Down
1 change: 0 additions & 1 deletion spec/mixinsSpec.coffee
@@ -1,4 +1,3 @@
_ = require 'underscore'
mixins = require '../lib/mixins'

describe "module exports", ->
Expand Down
19 changes: 15 additions & 4 deletions src/mixins.coffee
@@ -1,17 +1,18 @@
_ = require 'underscore'

###*
* Include a class or object with functions into another class.
* @param {Object} mixin [description]
* @param {Boolean} wrapOldFunction [description]
###

module.exports =
exports =
bootstrap: ->
Function::include = (mixin) ->
if not mixin
return throw 'Supplied mixin was not found'

if not _
return throw 'Underscore was not found'

mixin = mixin.prototype if _.isFunction(mixin)

# Make a copy of the superclass with the same constructor and use it instead of adding functions directly to the superclass.
Expand All @@ -29,4 +30,14 @@ module.exports =
@prototype[methodName] = funct

mixin.included?.apply(this)
this
this

if module?.exports?
if require?
_ = require 'underscore'

module.exports = exports
else
_ = window._

window.CoffeeScriptMixins = exports
61 changes: 61 additions & 0 deletions test/index.html
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Testing Mixins</title>
</head>

<body>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="../lib/mixins.js"></script>
<script>
$(function() {
window.CoffeeScriptMixins.bootstrap(); // Mix into Function.

var Mixin = function() {};

Mixin.prototype.sharedFunction = function() {
$('body').append($('<div>').text((new Date()).toString()));
};

var A = function() {};
var B = function() {};
var C = function() {};
var D = function() {};
var E = function() {};

C.prototype.sharedFunction = function() {
$('body').append($('<div>').text('Hey!'));
C.__super__.sharedFunction(); // super
};

D.prototype.sharedFunction = function() {
$('body').append($('<div>').text('Should not appear!'));
};

E.prototype.sharedFunction = function() {
$('body').append($('<div>').text('Should appear!'));
C.__super__.sharedFunction(); // super
};

A.include(Mixin);
B.include(Mixin);
C.include(Mixin);
E.include(Mixin);

var a = new A();
a.sharedFunction();

var b = new B();
b.sharedFunction();

var c = new C();
c.sharedFunction();

var e = new E();
e.sharedFunction();
});
</script>
</body>
</html>

0 comments on commit 2d193af

Please sign in to comment.