bbr-wsm 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +146 -0
- data/Rakefile +48 -0
- data/lib/backbone-rails.rb +6 -0
- data/lib/generators/backbone/install/install_generator.rb +42 -0
- data/lib/generators/backbone/install/templates/app.coffee +11 -0
- data/lib/generators/backbone/model/model_generator.rb +19 -0
- data/lib/generators/backbone/model/templates/model.coffee +11 -0
- data/lib/generators/backbone/resource_helpers.rb +55 -0
- data/lib/generators/backbone/router/router_generator.rb +44 -0
- data/lib/generators/backbone/router/templates/router.coffee +14 -0
- data/lib/generators/backbone/router/templates/template.jst +2 -0
- data/lib/generators/backbone/router/templates/view.coffee +8 -0
- data/lib/generators/backbone/scaffold/scaffold_generator.rb +31 -0
- data/lib/generators/backbone/scaffold/templates/model.coffee +11 -0
- data/lib/generators/backbone/scaffold/templates/router.coffee +31 -0
- data/lib/generators/backbone/scaffold/templates/templates/edit.jst +17 -0
- data/lib/generators/backbone/scaffold/templates/templates/index.jst +16 -0
- data/lib/generators/backbone/scaffold/templates/templates/model.jst +7 -0
- data/lib/generators/backbone/scaffold/templates/templates/new.jst +17 -0
- data/lib/generators/backbone/scaffold/templates/templates/show.jst +9 -0
- data/lib/generators/backbone/scaffold/templates/views/edit_view.coffee +24 -0
- data/lib/generators/backbone/scaffold/templates/views/index_view.coffee +20 -0
- data/lib/generators/backbone/scaffold/templates/views/model_view.coffee +19 -0
- data/lib/generators/backbone/scaffold/templates/views/new_view.coffee +37 -0
- data/lib/generators/backbone/scaffold/templates/views/show_view.coffee +8 -0
- data/lib/rails-backbone.rb +2 -0
- data/lib/tasks/backbone-rails_tasks.rake +4 -0
- data/vendor/assets/javascripts/backbone.js +1582 -0
- data/vendor/assets/javascripts/backbone_datalink.js +21 -0
- data/vendor/assets/javascripts/backbone_rails_sync.js +32 -0
- data/vendor/assets/javascripts/underscore.js +1276 -0
- metadata +216 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1f39089fa98ee0fea9e701fa501f3526f476ecef
|
4
|
+
data.tar.gz: 041b48535594d49ba963b0cacef2b5dee4378685
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7a4f2b06d8cd4a00e23ed11161df0cb60fdf6b5a56b3055357ff12815e19466991cf1c95b8b37865054560387509e7cb64524d21554b637427fcd2eac14d94e
|
7
|
+
data.tar.gz: 14c248985c15734ad97b8c5f3d9bdcc371a584d8b6f2c18fd49a11525d96ab7260802e102e423bc4edd4d1241bf6a9be886f219bf76e379b4c435e0d4eee9e60
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Ryan Fitzgerald
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# Backbone-Rails [](http://travis-ci.org/codebrew/backbone-rails)[](http://badge.fury.io/rb/rails-backbone)
|
2
|
+
|
3
|
+
Easily setup and use backbone.js (1.1.0) with Rails 3.1 and greater
|
4
|
+
|
5
|
+
##Version##
|
6
|
+
|
7
|
+
###Github master branch###
|
8
|
+
|
9
|
+
Gem version : 1.1.0
|
10
|
+
|
11
|
+
Backbone version : 1.1.0
|
12
|
+
|
13
|
+
Underscore version : 1.5.2
|
14
|
+
|
15
|
+
###Rubygems###
|
16
|
+
|
17
|
+
Gem version : 0.9.10
|
18
|
+
|
19
|
+
Backbone version : 0.9.10
|
20
|
+
|
21
|
+
Underscore version : 1.4.3
|
22
|
+
|
23
|
+
|
24
|
+
##Credits##
|
25
|
+
###Author###
|
26
|
+
[Ryan Fitzgerald](http://twitter.com/#!/TheRyanFitz)
|
27
|
+
###Current Maintainer
|
28
|
+
[Manu S Ajith](http://twitter.com/manusajith)
|
29
|
+
###Contributors###
|
30
|
+
These [awesome people](https://github.com/codebrew/backbone-rails/graphs/contributors) helped to keep this gem updated
|
31
|
+
|
32
|
+
## Rails setup
|
33
|
+
This gem requires the use of rails 3.1 and greater, coffeescript and the new rails asset pipeline provided by sprockets.
|
34
|
+
|
35
|
+
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.
|
36
|
+
|
37
|
+
### Installation
|
38
|
+
|
39
|
+
In your Gemfile, add this line:
|
40
|
+
|
41
|
+
gem "rails-backbone"
|
42
|
+
|
43
|
+
Then run the following commands:
|
44
|
+
|
45
|
+
bundle install
|
46
|
+
rails g backbone:install
|
47
|
+
|
48
|
+
### Layout and namespacing
|
49
|
+
|
50
|
+
Running `rails g backbone:install` will create the following directory structure under `app/assets/javascripts/backbone`:
|
51
|
+
|
52
|
+
routers/
|
53
|
+
models/
|
54
|
+
templates/
|
55
|
+
views/
|
56
|
+
|
57
|
+
It will also create a toplevel app_name.coffee file to setup namespacing and setup initial requires.
|
58
|
+
|
59
|
+
## Generators
|
60
|
+
backbone-rails provides 3 simple generators to help get you started using backbone.js with rails 3.1 and greater.
|
61
|
+
The generators will only create client side code (javascript).
|
62
|
+
|
63
|
+
### Model Generator
|
64
|
+
|
65
|
+
rails g backbone:model
|
66
|
+
|
67
|
+
This generator creates a backbone model and collection inside `app/assets/javascript/backbone/models` to be used to talk to the rails backend.
|
68
|
+
|
69
|
+
### Routers
|
70
|
+
|
71
|
+
rails g backbone:router
|
72
|
+
|
73
|
+
This generator creates a backbone router with corresponding views and templates for the given actions provided.
|
74
|
+
|
75
|
+
### Scaffolding
|
76
|
+
|
77
|
+
rails g backbone:scaffold
|
78
|
+
|
79
|
+
This generator creates a router, views, templates, model and collection to create a simple crud single page app
|
80
|
+
|
81
|
+
## Example Usage
|
82
|
+
|
83
|
+
Created a new rails application called `blog`.
|
84
|
+
|
85
|
+
rails new blog
|
86
|
+
|
87
|
+
Edit your Gemfile and add
|
88
|
+
|
89
|
+
gem 'rails-backbone'
|
90
|
+
|
91
|
+
Install the gem and generate scaffolding.
|
92
|
+
|
93
|
+
bundle install
|
94
|
+
rails g backbone:install
|
95
|
+
rails g scaffold Post title:string content:string
|
96
|
+
rake db:migrate
|
97
|
+
rails g backbone:scaffold Post title:string content:string
|
98
|
+
|
99
|
+
You now have installed the backbone-rails gem, setup a default directory structure for your frontend backbone code.
|
100
|
+
Then you generated the usual rails server side crud scaffolding and finally generated backbone.js code to provide a simple single page crud app.
|
101
|
+
You have one last step:
|
102
|
+
|
103
|
+
Edit your posts index view `app/views/posts/index.html.erb` with the following contents:
|
104
|
+
|
105
|
+
<div id="posts"></div>
|
106
|
+
|
107
|
+
<script type="text/javascript">
|
108
|
+
$(function() {
|
109
|
+
// Blog is the app name
|
110
|
+
window.router = new Blog.Routers.PostsRouter({posts: <%= @posts.to_json.html_safe -%>});
|
111
|
+
Backbone.history.start();
|
112
|
+
});
|
113
|
+
</script>
|
114
|
+
|
115
|
+
If you prefer haml, this is equivalent to inserting the following code into `app/views/posts/index.html.haml`:
|
116
|
+
|
117
|
+
#posts
|
118
|
+
|
119
|
+
:javascript
|
120
|
+
$(function() {
|
121
|
+
// Blog is the app name
|
122
|
+
window.router = new Blog.Routers.PostsRouter({posts: #{@posts.to_json.html_safe}});
|
123
|
+
Backbone.history.start();
|
124
|
+
});
|
125
|
+
|
126
|
+
|
127
|
+
Now start your server `rails s` and browse to [localhost:3000/posts](http://localhost:3000/posts)
|
128
|
+
You should now have a fully functioning single page crud app for Post models.
|
129
|
+
|
130
|
+
##Note:##
|
131
|
+
####Overrides backbone sync function####
|
132
|
+
This gem overrides the backbone sync function. Check [here](https://github.com/codebrew/backbone-rails/blob/master/vendor/assets/javascripts/backbone_rails_sync.js) for details.
|
133
|
+
|
134
|
+
####With Rails 4:####
|
135
|
+
If you are using the default Rails 4 scaffold generators, you will need to adjust the default JSON show view (IE, 'show.json') to render the 'id' attribute.
|
136
|
+
|
137
|
+
default rails generated show.json.jbuilder
|
138
|
+
|
139
|
+
`json.extract! @post, :title, :content, :created_at, :updated_at, :id`
|
140
|
+
|
141
|
+
Change it to add `id` attribute as well
|
142
|
+
|
143
|
+
`json.extract! @book, :id, :title, :author, :created_at, :updated_at`
|
144
|
+
|
145
|
+
Without adjusting the JSON show view, you will be redirected to a "undefined" url after creating an object.
|
146
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'bundler/gem_tasks'
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'BackboneRails'
|
14
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
|
21
|
+
Rake::TestTask.new(:test) do |t|
|
22
|
+
t.libs << 'lib'
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task :default => :test
|
29
|
+
|
30
|
+
namespace :backbone do
|
31
|
+
desc "Download the latest released versions of underscore and backbone.js"
|
32
|
+
task :download_latest do
|
33
|
+
files = {
|
34
|
+
'underscore.js'=>'http://underscorejs.org/underscore.js',
|
35
|
+
'backbone.js' => 'http://backbonejs.org/backbone.js'
|
36
|
+
}
|
37
|
+
|
38
|
+
vendor_dir = "vendor/assets/javascripts"
|
39
|
+
|
40
|
+
require 'open-uri'
|
41
|
+
files.each do |local,remote|
|
42
|
+
puts "Downloading #{local}"
|
43
|
+
File.open "#{vendor_dir}/#{local}", 'w' do |f|
|
44
|
+
f.write open(remote).read
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'generators/backbone/resource_helpers'
|
2
|
+
|
3
|
+
module Backbone
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
include Backbone::Generators::ResourceHelpers
|
7
|
+
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
9
|
+
|
10
|
+
desc "This generator installs backbone.js with a default folder layout in app/assets/javascripts/backbone"
|
11
|
+
|
12
|
+
class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
|
13
|
+
:desc => "Skip Git ignores and keeps"
|
14
|
+
|
15
|
+
def inject_backbone
|
16
|
+
# for JavaScript application.js manifest:
|
17
|
+
if File.exists? "#{Rails.root}/app/assets/javascripts/application.js"
|
18
|
+
append_to_file "app/assets/javascripts/application.js" do
|
19
|
+
"//= require underscore\n//= require backbone\n//= require backbone_rails_sync\n//= require backbone_datalink\n//= require backbone/#{application_name.underscore}\n"
|
20
|
+
end
|
21
|
+
# ...or for CoffeeScript application.js.coffee manifest:
|
22
|
+
elsif File.exists? "#{Rails.root}/app/assets/javascripts/application.js.coffee"
|
23
|
+
append_to_file "app/assets/javascripts/application.js.coffee" do
|
24
|
+
"\n#= require underscore\n#= require backbone\n#= require backbone_rails_sync\n#= require backbone_datalink\n#= require backbone/#{application_name.underscore}\n"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_dir_layout
|
30
|
+
%W{routers models views templates}.each do |dir|
|
31
|
+
empty_directory "app/assets/javascripts/backbone/#{dir}"
|
32
|
+
create_file "app/assets/javascripts/backbone/#{dir}/.gitkeep" unless options[:skip_git]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_app_file
|
37
|
+
template "app.coffee", "app/assets/javascripts/backbone/#{application_name.underscore}.js.coffee"
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'generators/backbone/resource_helpers'
|
2
|
+
|
3
|
+
module Backbone
|
4
|
+
module Generators
|
5
|
+
class ModelGenerator < Rails::Generators::NamedBase
|
6
|
+
include Backbone::Generators::ResourceHelpers
|
7
|
+
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
9
|
+
desc "This generator creates a backbone model"
|
10
|
+
|
11
|
+
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
|
12
|
+
|
13
|
+
def create_backbone_model
|
14
|
+
template "model.coffee", "#{backbone_path}/models/#{file_name}.js.coffee"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class <%= model_namespace %> extends Backbone.Model
|
2
|
+
paramRoot: '<%= singular_table_name %>'
|
3
|
+
|
4
|
+
defaults:
|
5
|
+
<% attributes.each do |attribute| -%>
|
6
|
+
<%= attribute.name %>: null
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
class <%= collection_namespace %>Collection extends Backbone.Collection
|
10
|
+
model: <%= model_namespace %>
|
11
|
+
url: '<%= route_url %>'
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Backbone
|
2
|
+
module Generators
|
3
|
+
module ResourceHelpers
|
4
|
+
|
5
|
+
def backbone_path
|
6
|
+
"app/assets/javascripts/backbone"
|
7
|
+
end
|
8
|
+
|
9
|
+
def model_namespace
|
10
|
+
[js_app_name, "Models", class_name].join(".")
|
11
|
+
end
|
12
|
+
|
13
|
+
def singular_model_name
|
14
|
+
uncapitalize singular_name.camelize
|
15
|
+
end
|
16
|
+
|
17
|
+
def plural_model_name
|
18
|
+
uncapitalize(plural_name.camelize)
|
19
|
+
end
|
20
|
+
|
21
|
+
def collection_namespace
|
22
|
+
[js_app_name, "Collections", plural_name.camelize].join(".")
|
23
|
+
end
|
24
|
+
|
25
|
+
def view_namespace
|
26
|
+
[js_app_name, "Views", plural_name.camelize].join(".")
|
27
|
+
end
|
28
|
+
|
29
|
+
def router_namespace
|
30
|
+
[js_app_name, "Routers", plural_name.camelize].join(".")
|
31
|
+
end
|
32
|
+
|
33
|
+
def jst(action)
|
34
|
+
"backbone/templates/#{plural_name}/#{action}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def js_app_name
|
38
|
+
application_name.camelize
|
39
|
+
end
|
40
|
+
|
41
|
+
def application_name
|
42
|
+
if defined?(Rails) && Rails.application
|
43
|
+
Rails.application.class.name.split('::').first
|
44
|
+
else
|
45
|
+
"application"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def uncapitalize(str)
|
50
|
+
str[0, 1].downcase << str[1..-1]
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'generators/backbone/resource_helpers'
|
2
|
+
|
3
|
+
module Backbone
|
4
|
+
module Generators
|
5
|
+
class RouterGenerator < Rails::Generators::NamedBase
|
6
|
+
include Backbone::Generators::ResourceHelpers
|
7
|
+
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
9
|
+
desc "This generator creates a backbone router with views and templates for the provided actions"
|
10
|
+
|
11
|
+
argument :actions, :type => :array, :default => [], :banner => "action action"
|
12
|
+
|
13
|
+
RESERVED_JS_WORDS = %W{
|
14
|
+
break case catch continue debugger default delete do else finally for
|
15
|
+
function if in instanceof new return switch this throw try typeof var void while with
|
16
|
+
}
|
17
|
+
|
18
|
+
def validate_no_reserved_words
|
19
|
+
actions.each do |action|
|
20
|
+
if RESERVED_JS_WORDS.include? action
|
21
|
+
raise Thor::Error, "The name '#{action}' is reserved by javascript " <<
|
22
|
+
"Please choose an alternative action name and run this generator again."
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_router_files
|
28
|
+
template 'router.coffee', File.join(backbone_path, "routers", class_path, "#{file_name}_router.js.coffee")
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_view_files
|
32
|
+
actions.each do |action|
|
33
|
+
@action = action
|
34
|
+
@view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.js.coffee")
|
35
|
+
@jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.jst.ejs")
|
36
|
+
|
37
|
+
template "view.coffee", @view_path
|
38
|
+
template "template.jst", @jst_path
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class <%= router_namespace %>Router extends Backbone.Router
|
2
|
+
initialize: (options) ->
|
3
|
+
|
4
|
+
routes:
|
5
|
+
<% actions.each do |action| -%>
|
6
|
+
"<%= action %>": "<%= action %>"
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
<% actions.each do |action| -%>
|
10
|
+
<%= action %>: ->
|
11
|
+
@view = new <%= "#{view_namespace}.#{action.camelize}View()" %>
|
12
|
+
$("#<%= plural_name %>").html(@view.render().el)
|
13
|
+
|
14
|
+
<% end -%>
|