Git Product home page Git Product logo

ember-rails's Introduction

ember-rails Build Status Dependency Status

ember-rails makes developing an Ember.JS application much easier in Rails 3.1+.

The following functionalities are included in this gem:

  • Pre-compiling of your handlebars templates when building your asset pipeline.
  • Includes development and production copies of Ember, Ember Data and Handlebars.
  • Includes ActiveModel::Serializer for integration with Ember Data.

You can see an example of how to use the gem here. There is also a great tutorial by Dan Gebhardt called "Beginning Ember.js on Rails" which is a great read if you're just starting out with Rails and Ember.js

Getting started

  1. Add the gem to your application Gemfile:
gem 'ember-rails'
gem 'ember-source', '1.1.0' # or the version you need

# optional since Handlebars 1.0.0 was released
#gem 'handlebars-source', '~> 1.0.12' # or the version you need
  1. Run bundle install

  2. Configure the ember variant in your environment files (i.e. development.rb, production.rb). If you don't configure this, the version of Ember used defaults to development when the Rails environment is development, and similarly for production.

  config.ember.variant = :development # or :production
  1. Next, generate the application structure:
rails generate ember:bootstrap
  1. Restart your server (if it's running)

Notes:

To install the latest builds of ember and ember-data. It should be noted that the examples in the getting started guide have been designed to use the latest version of ember:

rails generate ember:install --head

You'll probably need to clear out your cache after doing this with

rake tmp:clear

For CoffeeScript support

  1. Add coffee-rails to the Gemfile
gem 'coffee-rails'
  1. Run the bootstrap generator in step 4 with an extra flag instead:
rails g ember:bootstrap -g --javascript-engine coffee

Note:

Ember-rails include some flags options for bootstrap generator:

--ember-path or -d # custom ember path
--skip-git or -g # skip git keeps
--javascript-engine  # engine for javascript (js or coffee)
--app-name or -n # custom ember app name

Architecture

Ember does not require an organized file structure. However, ember-rails allows you to use rails g ember:bootstrap to create the following directory structure under app/assets/javascripts:

controllers/
helpers/
components/
models/
routes/
templates/
templates/components
views/

Additionally, it will add the following lines to app/assets/javascripts/application.js. By default, it uses the Rails Application's name and creates an rails_app_name.js file to setup application namespace and initial requires:

//= require handlebars
//= require ember
//= require ember-data
//= require_self
//= require rails_app_name
RailsAppName = Ember.Application.create();

Example:

rails g ember:bootstrap
  insert  app/assets/javascripts/application.js
  create  app/assets/javascripts/models
  create  app/assets/javascripts/models/.gitkeep
  create  app/assets/javascripts/controllers
  create  app/assets/javascripts/controllers/.gitkeep
  create  app/assets/javascripts/views
  create  app/assets/javascripts/views/.gitkeep
  create  app/assets/javascripts/helpers
  create  app/assets/javascripts/helpers/.gitkeep
  create  app/assets/javascripts/components
  create  app/assets/javascripts/components/.gitkeep
  create  app/assets/javascripts/templates
  create  app/assets/javascripts/templates/.gitkeep
  create  app/assets/javascripts/templates/components
  create  app/assets/javascripts/templates/components/.gitkeep
  create  app/assets/javascripts/app.js

If you want to avoid .gitkeep files, use the skip git option like this: rails g ember:bootstrap -g.

Ask Rails to serve HandlebarsJS and pre-compile templates to Ember by putting each template in a dedicated ".js.hjs", ".hbs" or ".handlebars" file (e.g. app/assets/javascripts/templates/admin_panel.handlebars) and including the assets in your layout:

<%= javascript_include_tag "templates/admin_panel" %>

If you want to strip template root from template names, add templates_root option to your application configuration block. By default, templates_root is 'templates'.

config.handlebars.templates_root = 'ember_templates'

If you store templates in a file like app/assets/javascripts/ember_templates/admin_panel.handlebars after setting the above config, it will be made available to Ember as the admin_panel template.

(Note: you must clear the local sprockets cache after modifying templates_root, stored by default in tmp/cache/assets)

Default behavior for ember-rails is to precompile handlebars templates. If you don't want this behavior you can turn it off in your application configuration (or per environment in: config/environments/development.rb) block:

config.handlebars.precompile = false

(Note: you must clear the local sprockets cache if you disable precompilation, stored by default in tmp/cache/assets)

Bundle all templates together thanks to Sprockets, e.g create app/assets/javascripts/templates/all.js with:

//= require_tree .

Now a single line in the layout loads everything:

<%= javascript_include_tag "templates/all" %>

If you use Slim or Haml templates, you can use handlebars filter :

handlebars:
    <button {{action anActionName}}>OK</button>

It will be translated as :

<script type="text/x-handlebars">
    <button {{action anActionName}}>OK</button>
</script>

Note about ember components

When necessary, ember-rails adheres to a conventional folder structure. To create an ember component you must define the handlebars file inside the components folder under the templates folder of your project to properly register your handlebars component file.

Example

With the following folder structure:

controllers/
helpers/
models/
routes/
templates/
  components/
    my-component.handlebars
views/

and a my-component.handlebars file with the following contents

<h1>My Component</h1>

will produce the following handlebars output

<script type="text/x-handlebars" id="components/my-component">
  <h1>My Component</h1>
</script>

You can reference your component inside your other handlebars template files by the handlebars file name:

 {{ my-component }}

Specifying Different Versions of Ember/Handlebars/Ember-Data

By default, ember-rails ships with the latest version of Ember, Handlebars, and Ember-Data.

To specify a different version that'll be used for both template precompilation and serving to the browser, you can specify the desired version of one of the above-linked gems in the Gemfile, e.g.:

gem 'ember-source', '1.0.0'

You can also specify versions of 'handlebars-source' and 'ember-data-source', but note that an appropriate 'handlebars-source' will be automatically chosen depending on the version of 'ember-source' that's specified.

You can also override the specific ember.js, handlebars.js, and ember-data.js files that'll be required by the Asset pipeline by placing these files in vendor/assets/ember/development and vendor/assets/ember/production, depending on the config.ember.variant you've specified in your app's configuration, e.g.:

config.ember.variant = :production
#config.ember.variant = :development

Updating Ember

If at any point you need to update Ember.js from master, you can do that with

rails generate ember:install --head

This will fetch both Ember.js and Ember Data from http://builds.emberjs.com/ and copy to the right directory.

Note on Patches/Pull Requests

  1. Fork the project.
  2. Make your feature addition or bug fix.
  3. Add tests for it. This is important so I don't break it in a future version unintentionally.
  4. Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  5. Send me a pull request. Bonus points for topic branches.

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.