GithubHelp home page GithubHelp logo

hasimo / backbone-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codebrew/backbone-rails

1.0 2.0 1.0 345 KB

Easily use backbone.js with rails 3.1

License: MIT License

Shell 4.03% Ruby 70.61% CoffeeScript 15.72% JavaScript 0.73% CSS 2.35% HTML 6.55%

backbone-rails's Introduction

Backbone-Rails Build Status

Easily setup and use backbone.js (0.9.10) with Rails 3.1 and greater

Follow @TheRyanFitz on Twitter. Tweet any questions or suggestions you have about the project.

Rails setup

This gem requires the use of rails 3.1 and greater, coffeescript and the new rails asset pipeline provided by sprockets.

This gem vendors the latest version of underscore.js and backbone.js for Rails 3.1 and greater. The files will be added to the asset pipeline and available for you to use.

Installation

In your Gemfile, add this line:

gem "rails-backbone"

Then run the following commands:

bundle install
rails g backbone:install

Layout and namespacing

Running rails g backbone:install will create the following directory structure under app/assets/javascripts/backbone:

routers/
models/
templates/
views/

It will also create a toplevel app_name.coffee file to setup namespacing and setup initial requires.

Generators

backbone-rails provides 3 simple generators to help get you started using backbone.js with rails 3.1 and greater. The generators will only create client side code (javascript).

Model Generator

rails g backbone:model

This generator creates a backbone model and collection inside app/assets/javascript/backbone/models to be used to talk to the rails backend.

Routers

rails g backbone:router

This generator creates a backbone router with corresponding views and templates for the given actions provided.

Scaffolding

rails g backbone:scaffold

This generator creates a router, views, templates, model and collection to create a simple crud single page app

Example Usage

Created a new rails application called blog.

rails new blog

Edit your Gemfile and add

gem 'rails-backbone'

Install the gem and generate scaffolding.

bundle install
rails g backbone:install
rails g scaffold Post title:string content:string
rake db:migrate
rails g backbone:scaffold Post title:string content:string

You now have installed the backbone-rails gem, setup a default directory structure for your frontend backbone code. Then you generated the usual rails server side crud scaffolding and finally generated backbone.js code to provide a simple single page crud app. You have one last step:

Edit your posts index view app/views/posts/index.html.erb with the following contents:

<div id="posts"></div>

<script type="text/javascript">
  $(function() {
    // Blog is the app name
    window.router = new Blog.Routers.PostsRouter({posts: <%= @posts.to_json.html_safe -%>});
    Backbone.history.start();
  });
</script>

If you prefer haml, this is equivalent to inserting the following code into app/views/posts/index.html.haml:

:javascript
  $(function() {
    // Blog is the app name
    window.router = new Blog.Routers.PostsRouter({posts: #{@posts.to_json.html_safe}});
    Backbone.history.start();
  });

Now start your server rails s and browse to localhost:3000/posts You should now have a fully functioning single page crud app for Post models.

backbone-rails's People

Contributors

andriytyurnikov avatar bmaland avatar bnmrrs avatar chrismytton avatar chrisnicola avatar codebrew avatar dlt avatar dnagir avatar drewda avatar dvgica avatar eric-hu avatar gilesbowkett avatar joliss avatar mkelley33 avatar mplatts avatar rjfranco avatar robharper avatar swhitt avatar thluiz avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

mcfiredrill

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.