Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Adds the ability to include Modules into JavaScript objects
Ruby CoffeeScript
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec/javascripts
src
vendor/assets/javascripts
.gitignore
Gemfile
Gemfile.lock
Guardfile
LICENSE
README.md
Rakefile
includejs.gemspec

README.md

Includejs

Allows you to inject functions and properties from other JavaScript objects into your current object. This is similar to the functionality provided by _.extend in the Underscore.js library, the difference being that Includejs will bind the functions to the current object, helpful well dealing with callback functions.

Installation

Ruby/Rails

Add this line to your application's Gemfile:

gem 'includejs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install includejs

Rails

Require includejs in your asset pipeline and you're ready to go.

Non-Rails

To use this outside of a Rails application simply copy the vendor/assets/javascripts/includejs.js file and add it to your application. That's it.

Usage

CoffeeScript Example:

BazModule =
  sayHi: ->
    "Hello #{@name}!"
  # If there is an 'included' function it will
  # be called when the module is included.
  included: (klass) ->
    klass.baz = "Baz!!"

class Foo
  constructor: ->
    include(@, BazModule)

foo = new Foo()
foo.sayHi() # => "Hello undefined!"

foo.name = "Mark"
foo.sayHi() # => "Hello Mark!"

JavaScript Example:

var BazModule, foo;

BazModule = {
  sayHi: function() {
    return "Hello " + this.name + "!";
  },
  // If there is an 'included' function it will
  // be called when the module is included.
  included: function(klass) {
    klass.baz = "Baz!!";
  }
};

foo = {}
include(foo, BazModule);

foo.sayHi(); // => "Hello undefined!"

foo.name = "Mark";

foo.sayHi(); // => "Hello Mark!"

Testing

  1. Run bundle install.
  2. Run guard or alternatively rake.
  3. Write your tests. No pull request will be accepted without tests.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.