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
- Run
bundle install
. - Run
guard
or alternativelyrake
. - Write your tests. No pull request will be accepted without tests.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request