GithubHelp home page GithubHelp logo

stitch_rails's Introduction

stitch_rails

Inspired by https://gist.github.com/1153919

Brings require to Rails Sprockets.

All .coffee file will be wrapped as require.define.

require("foo/bar") will load file app/assets/javascripts/foo/bar.js.coffee and execute it.

Example

Gemfile:

group :assets do
  gem 'stitch_rails', '~>0.0.7'
end

config/application.rb:

module YourApp
  class Application < Rails::Application
    # .... omit ....

    # file names listed here are not wraped in `require.define`, so they
    # are executed immediatly. Handy for an app start point. Otherwise you
    # have add an .js file to require your start file. Suffix should not be added here.
    # Glob pattern is also supported.
    config.stitch.excludes = %w(application *_spec jasminerice)
  end
end

app/assets/javascripts/application.js.coffee:

# These are sprockets require that used to includes js files. Since
# .coffee file are wrapped as module, the js code is only executed when
# javascript function `require` is invoked.
#
#= require jquery
#
# Must load stitch_rails before any other coffee file, it defines the global method `require`
#
#= require stitch_rails
#= require_tree ./
#= require_this
#
# Since this file is in stitch excludes, code here are executed when it is loaded:

$ ->
  Greet = require('models/greet')
  greet = new Greet()
  greet.sayHi()

app/assets/javascripts/models/greet.js.coffee

class Greet
  sayHi: ->
    alert 'Hello, World'

module.exports = Greet

See more about CommonJS module in http://wiki.commonjs.org/wiki/Modules/1.1

TODO

  • Make it work for Sprockets without Rails

stitch_rails's People

Contributors

doitian avatar blasut avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.