railsy_backbone 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (25) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +34 -6
  3. data/README.md +1 -1
  4. data/lib/generators/backbone/model/model_generator.rb +19 -0
  5. data/lib/generators/backbone/model/templates/model.coffee +11 -0
  6. data/lib/generators/backbone/router/router_generator.rb +44 -0
  7. data/lib/generators/backbone/router/templates/router.coffee +14 -0
  8. data/lib/generators/backbone/router/templates/template.jst +2 -0
  9. data/lib/generators/backbone/router/templates/view.coffee +8 -0
  10. data/lib/generators/backbone/scaffold/scaffold_generator.rb +31 -0
  11. data/lib/generators/backbone/scaffold/templates/model.coffee +11 -0
  12. data/lib/generators/backbone/scaffold/templates/router.coffee +31 -0
  13. data/lib/generators/backbone/scaffold/templates/templates/edit.jst +17 -0
  14. data/lib/generators/backbone/scaffold/templates/templates/index.jst +16 -0
  15. data/lib/generators/backbone/scaffold/templates/templates/model.jst +7 -0
  16. data/lib/generators/backbone/scaffold/templates/templates/new.jst +17 -0
  17. data/lib/generators/backbone/scaffold/templates/templates/show.jst +9 -0
  18. data/lib/generators/backbone/scaffold/templates/views/edit_view.coffee +24 -0
  19. data/lib/generators/backbone/scaffold/templates/views/index_view.coffee +20 -0
  20. data/lib/generators/backbone/scaffold/templates/views/model_view.coffee +19 -0
  21. data/lib/generators/backbone/scaffold/templates/views/new_view.coffee +37 -0
  22. data/lib/generators/backbone/scaffold/templates/views/show_view.coffee +8 -0
  23. data/lib/railsy_backbone/version.rb +1 -1
  24. data/vendor/assets/javascripts/railsy_backbone.datalink.js +40 -25
  25. metadata +21 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb1af4f0c455456434c50c08a1d5223423504b6b
4
- data.tar.gz: c1ae2f4fdff40e8cb05baac296e99e196bec3240
3
+ metadata.gz: a846e920a5dad426c7c7e892cf4af5c5e7a85e45
4
+ data.tar.gz: 37d9f658a1c1b61f45aa144a5393429134ac6474
5
5
  SHA512:
6
- metadata.gz: 40ce46134b4a21e1a576a86625793b1431ae6bce5db3b5cea8b789fb203f85012590ef1f08b4f66f3807e2b0d9eaa7cd130f65b88ffef08abb32aaf75c83f4dd
7
- data.tar.gz: b4678c7c8ca149d574d62ac03d2c42a2edc88da9fe96f302b48a94365a528a3ef89373714ad0dcd207fc1484010b6821c527946ab1d5e8dfb739cc5c428f5c79
6
+ metadata.gz: 5fae32e260f47e893775010bbe983361126644085e70391d5013b533d6c586f53a883fc6eef3e6a349aad38339e76369b0308535a4d57b8be55e662a01bdd54d
7
+ data.tar.gz: fd9f382bbbe0a082ceafdc937ba33dd80d2f7de9f3a033d129c07a763ae176086d7fb93995296b66f237d0d9c727341c1f720dd853d4772faf7e286531e44dbb
data/CHANGELOG.md CHANGED
@@ -1,26 +1,54 @@
1
+ ## 0.0.3
2
+
3
+ - copied Backbone model generator from backbone-rails
4
+ (westonplatter)
5
+
6
+ - copied Backbone router generator from backbone-rails
7
+ (westonplatter)
8
+
9
+ - copied Backbone scaffold generator from backbone-rails
10
+ (westonplatter)
11
+
12
+ - copied the localized Gemset setup from backbone-rails
13
+ (westonplatter)
14
+
15
+ - copied `dataLink` from backbone-rails + added docs with TODOS
16
+
17
+ - updated install instructions
18
+
1
19
 
2
20
  ## 0.0.2
3
21
 
4
22
  - changed license to BSD-3
23
+ (westonplatter)
5
24
 
6
25
  - setup [travis ci](https://travis-ci.org/westonplatter/railsy_backbone)
26
+ (westonplatter)
7
27
 
8
28
  - application.js install generator is fully setup
9
-
29
+ (westonplatter)
30
+
10
31
  - application.js install generator is full tested
11
-
32
+ (westonplatter)
33
+
12
34
  - moved location of helpers.rb so it can be required
13
-
14
- - setup test unit (much thanks to [Ryan Fitzgerald](https://twitter.com/TheRyanFitz))
35
+ (westonplatter)
15
36
 
37
+ - setup test unit (much thanks to [Ryan Fitzgerald](https://twitter.com/TheRyanFitz))
38
+ (westonplatter)
39
+
16
40
  - updated gemspec for jquery-rails 3
17
-
41
+ (westonplatter)
42
+
18
43
  - setup groc to document the project. docs available at
19
44
  [http://westonplatter.github.io/railsy_backbone](http://westonplatter.github.io/railsy_backbone)
45
+ (westonplatter)
20
46
 
21
47
 
22
48
  ## 0.0.1
23
49
 
24
50
  - Updated jquery-rails to be 2.2.x compatible.
25
-
51
+ (westonplatter)
52
+
26
53
  - Copied repo from https://github.com/codebrew/backbone-rails
54
+ (westonplatter)
data/README.md CHANGED
@@ -40,7 +40,7 @@ Automatically handles the Rails `authenticity_token`. Or, more technically, sets
40
40
 
41
41
  Add this line to your application's Gemfile:
42
42
 
43
- gem 'railsy_backbone', github: 'westonplatter/railsy_backbone'
43
+ gem 'railsy_backbone', '~> 0.0.3'
44
44
 
45
45
  And then execute:
46
46
 
@@ -0,0 +1,19 @@
1
+ require 'generators/backbone/helpers'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class ModelGenerator < Rails::Generators::NamedBase
6
+ include Backbone::Generators::Helpers
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,44 @@
1
+ require 'generators/backbone/helpers'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class RouterGenerator < Rails::Generators::NamedBase
6
+ include Backbone::Generators::Helpers
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 -%>
@@ -0,0 +1,2 @@
1
+ <h1><%= class_name %>#<%= @action %></h1>
2
+ <p>Find me in <%= @jst_path %></p>
@@ -0,0 +1,8 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.<%= @action.camelize %>View extends Backbone.View
4
+ template: JST["<%= jst @action %>"]
5
+
6
+ render: ->
7
+ @$el.html(@template())
8
+ return this
@@ -0,0 +1,31 @@
1
+ require 'generators/backbone/model/model_generator'
2
+
3
+ module Backbone
4
+ module Generators
5
+ class ScaffoldGenerator < ModelGenerator
6
+
7
+ source_root File.expand_path("../templates", __FILE__)
8
+ desc "This generator creates the client side crud scaffolding"
9
+
10
+ def create_router_files
11
+ template 'router.coffee', File.join(backbone_path, "routers", class_path, "#{plural_name}_router.js.coffee")
12
+ end
13
+
14
+ def create_view_files
15
+ available_views.each do |view|
16
+ template "views/#{view}_view.coffee", File.join(backbone_path, "views", plural_name, "#{view}_view.js.coffee")
17
+ template "templates/#{view}.jst", File.join(backbone_path, "templates", plural_name, "#{view}.jst.ejs")
18
+ end
19
+
20
+ template "views/model_view.coffee", File.join(backbone_path, "views", plural_name, "#{singular_name}_view.js.coffee")
21
+ template "templates/model.jst", File.join(backbone_path, "templates", plural_name, "#{singular_name}.jst.ejs")
22
+ end
23
+
24
+ protected
25
+ def available_views
26
+ %w(index show new edit)
27
+ end
28
+
29
+ end
30
+ end
31
+ 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,31 @@
1
+ class <%= router_namespace %>Router extends Backbone.Router
2
+ initialize: (options) ->
3
+ @<%= plural_model_name %> = new <%= collection_namespace %>Collection()
4
+ @<%= plural_model_name %>.reset options.<%= plural_model_name %>
5
+
6
+ routes:
7
+ "new" : "new<%= class_name %>"
8
+ "index" : "index"
9
+ ":id/edit" : "edit"
10
+ ":id" : "show"
11
+ ".*" : "index"
12
+
13
+ new<%= class_name %>: ->
14
+ @view = new <%= "#{view_namespace}.NewView(collection: @#{plural_name})" %>
15
+ $("#<%= plural_name %>").html(@view.render().el)
16
+
17
+ index: ->
18
+ @view = new <%= "#{view_namespace}.IndexView(#{plural_name}: @#{plural_name})" %>
19
+ $("#<%= plural_name %>").html(@view.render().el)
20
+
21
+ show: (id) ->
22
+ <%= singular_name %> = @<%= plural_name %>.get(id)
23
+
24
+ @view = new <%= "#{view_namespace}.ShowView(model: #{singular_name})" %>
25
+ $("#<%= plural_name %>").html(@view.render().el)
26
+
27
+ edit: (id) ->
28
+ <%= singular_name %> = @<%= plural_name %>.get(id)
29
+
30
+ @view = new <%= "#{view_namespace}.EditView(model: #{singular_name})" %>
31
+ $("#<%= plural_name %>").html(@view.render().el)
@@ -0,0 +1,17 @@
1
+ <h1>Edit <%= singular_table_name %></h1>
2
+
3
+ <form id="edit-<%= singular_table_name %>" name="<%= singular_table_name %>">
4
+ <% attributes.each do |attribute| -%>
5
+ <div class="field">
6
+ <label for="<%= attribute.name %>"> <%= attribute.name %>:</label>
7
+ <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" >
8
+ </div>
9
+
10
+ <% end -%>
11
+ <div class="actions">
12
+ <input type="submit" value="Update <%= human_name %>" />
13
+ </div>
14
+
15
+ </form>
16
+
17
+ <a href="#/index">Back</a>
@@ -0,0 +1,16 @@
1
+ <h1>Listing <%= plural_table_name %></h1>
2
+
3
+ <table id="<%= plural_name %>-table">
4
+ <tr>
5
+ <% attributes.each do |attribute| -%>
6
+ <th><%= attribute.human_name %></th>
7
+ <% end -%>
8
+ <th></th>
9
+ <th></th>
10
+ <th></th>
11
+ </tr>
12
+ </table>
13
+
14
+ <br/>
15
+
16
+ <a href="#/new">New <%= human_name %></a>
@@ -0,0 +1,7 @@
1
+ <% attributes.each do |attribute| -%>
2
+ <td><%%= <%= attribute.name %> %></td>
3
+ <% end -%>
4
+
5
+ <td><a href="#/<%%= id %>">Show</td>
6
+ <td><a href="#/<%%= id %>/edit">Edit</td>
7
+ <td><a href="#/<%%= id %>/destroy" class="destroy">Destroy</a></td>
@@ -0,0 +1,17 @@
1
+ <h1>New <%= singular_table_name %></h1>
2
+
3
+ <form id="new-<%= singular_table_name %>" name="<%= singular_table_name %>">
4
+ <% attributes.each do |attribute| -%>
5
+ <div class="field">
6
+ <label for="<%= attribute.name %>"> <%= attribute.name %>:</label>
7
+ <input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="<%%= <%= attribute.name %> %>" >
8
+ </div>
9
+
10
+ <% end -%>
11
+ <div class="actions">
12
+ <input type="submit" value="Create <%= human_name %>" />
13
+ </div>
14
+
15
+ </form>
16
+
17
+ <a href="#/index">Back</a>
@@ -0,0 +1,9 @@
1
+ <% attributes.each do |attribute| -%>
2
+ <p>
3
+ <b><%= attribute.human_name %>:</b>
4
+ <%%= <%= attribute.name %> %>
5
+ </p>
6
+
7
+ <% end -%>
8
+
9
+ <a href="#/index">Back</a>
@@ -0,0 +1,24 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.EditView extends Backbone.View
4
+ template: JST["<%= jst 'edit' %>"]
5
+
6
+ events:
7
+ "submit #edit-<%= singular_name %>": "update"
8
+
9
+ update: (e) ->
10
+ e.preventDefault()
11
+ e.stopPropagation()
12
+
13
+ @model.save(null,
14
+ success: (<%= singular_name %>) =>
15
+ @model = <%= singular_name %>
16
+ window.location.hash = "/#{@model.id}"
17
+ )
18
+
19
+ render: ->
20
+ @$el.html(@template(@model.toJSON() ))
21
+
22
+ this.$("form").backboneLink(@model)
23
+
24
+ return this
@@ -0,0 +1,20 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.IndexView extends Backbone.View
4
+ template: JST["<%= jst 'index' %>"]
5
+
6
+ initialize: () ->
7
+ @options.<%= plural_model_name %>.bind('reset', @addAll)
8
+
9
+ addAll: () =>
10
+ @options.<%= plural_model_name %>.each(@addOne)
11
+
12
+ addOne: (<%= singular_model_name %>) =>
13
+ view = new <%= view_namespace %>.<%= singular_name.camelize %>View({model : <%= singular_model_name %>})
14
+ @$("tbody").append(view.render().el)
15
+
16
+ render: =>
17
+ @$el.html(@template(<%= plural_model_name %>: @options.<%= plural_model_name %>.toJSON() ))
18
+ @addAll()
19
+
20
+ return this
@@ -0,0 +1,19 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.<%= singular_name.camelize %>View extends Backbone.View
4
+ template: JST["<%= jst singular_name %>"]
5
+
6
+ events:
7
+ "click .destroy" : "destroy"
8
+
9
+ tagName: "tr"
10
+
11
+ destroy: () ->
12
+ @model.destroy()
13
+ this.remove()
14
+
15
+ return false
16
+
17
+ render: ->
18
+ @$el.html(@template(@model.toJSON() ))
19
+ return this
@@ -0,0 +1,37 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.NewView extends Backbone.View
4
+ template: JST["<%= jst 'new' %>"]
5
+
6
+ events:
7
+ "submit #new-<%= singular_name %>": "save"
8
+
9
+ constructor: (options) ->
10
+ super(options)
11
+ @model = new @collection.model()
12
+
13
+ @model.bind("change:errors", () =>
14
+ this.render()
15
+ )
16
+
17
+ save: (e) ->
18
+ e.preventDefault()
19
+ e.stopPropagation()
20
+
21
+ @model.unset("errors")
22
+
23
+ @collection.create(@model.toJSON(),
24
+ success: (<%= singular_name %>) =>
25
+ @model = <%= singular_name %>
26
+ window.location.hash = "/#{@model.id}"
27
+
28
+ error: (<%= singular_name %>, jqXHR) =>
29
+ @model.set({errors: $.parseJSON(jqXHR.responseText)})
30
+ )
31
+
32
+ render: ->
33
+ @$el.html(@template(@model.toJSON() ))
34
+
35
+ this.$("form").backboneLink(@model)
36
+
37
+ return this
@@ -0,0 +1,8 @@
1
+ <%= view_namespace %> ||= {}
2
+
3
+ class <%= view_namespace %>.ShowView extends Backbone.View
4
+ template: JST["<%= jst 'show' %>"]
5
+
6
+ render: ->
7
+ @$el.html(@template(@model.toJSON() ))
8
+ return this
@@ -1,3 +1,3 @@
1
1
  module RailsyBackbone
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,28 +1,43 @@
1
1
  // __NOTE:__ only overriding Backbone when `railsy_backbone (start) ... (end)`
2
2
  // is explicitly called out.
3
3
  //
4
- // Before addign this to the repo, I'd like to enable a configuration value to
5
- // turn this feature on and off
6
- //
7
- //
8
- // (function($) {
9
- // return $.extend($.fn, {
10
- // backboneLink: function(model) {
11
- // return $(this).find(":input").each(function() {
12
- // var el, name;
13
- // el = $(this);
14
- // name = el.attr("name");
15
- // model.bind("change:" + name, function() {
16
- // return el.val(model.get(name));
17
- // });
18
- // return $(this).bind("change", function() {
19
- // var attrs;
20
- // el = $(this);
21
- // attrs = {};
22
- // attrs[el.attr("name")] = el.val();
23
- // return model.set(attrs);
24
- // });
25
- // });
26
- // }
27
- // });
28
- // })(jQuery);
4
+ // TODO
5
+ // Enable a configuration value to turn this feature on/off
6
+
7
+ (function($) {
8
+
9
+ // `$.extend` enables us to chain functions onto jQuery selected DOM elements.
10
+ // For example,
11
+ //
12
+ // this.$("form").backboneLink(@model)
13
+ //
14
+ return $.extend($.fn, {
15
+
16
+ // name function `backboneLine`
17
+ backboneLink: function(model) {
18
+
19
+ // only target HTML input elements
20
+ return $(this).find(":input").each(function() {
21
+ var el, name;
22
+ el = $(this);
23
+
24
+ // select the HTML input `name` attribute
25
+ name = el.attr("name");
26
+
27
+ // TODO describe what this does
28
+ model.bind("change:" + name, function() {
29
+ return el.val(model.get(name));
30
+ });
31
+
32
+ // re-set model specifical attribute when changed
33
+ return $(this).bind("change", function() {
34
+ var attrs;
35
+ el = $(this);
36
+ attrs = {};
37
+ attrs[el.attr("name")] = el.val();
38
+ return model.set(attrs);
39
+ });
40
+ });
41
+ }
42
+ });
43
+ })(jQuery);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railsy_backbone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Platter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
11
+ date: 2013-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -202,6 +202,25 @@ files:
202
202
  - lib/generators/backbone/helpers.rb
203
203
  - lib/generators/backbone/install/install_generator.rb
204
204
  - lib/generators/backbone/install/templates/app.coffee
205
+ - lib/generators/backbone/model/model_generator.rb
206
+ - lib/generators/backbone/model/templates/model.coffee
207
+ - lib/generators/backbone/router/router_generator.rb
208
+ - lib/generators/backbone/router/templates/router.coffee
209
+ - lib/generators/backbone/router/templates/template.jst
210
+ - lib/generators/backbone/router/templates/view.coffee
211
+ - lib/generators/backbone/scaffold/scaffold_generator.rb
212
+ - lib/generators/backbone/scaffold/templates/model.coffee
213
+ - lib/generators/backbone/scaffold/templates/router.coffee
214
+ - lib/generators/backbone/scaffold/templates/templates/edit.jst
215
+ - lib/generators/backbone/scaffold/templates/templates/index.jst
216
+ - lib/generators/backbone/scaffold/templates/templates/model.jst
217
+ - lib/generators/backbone/scaffold/templates/templates/new.jst
218
+ - lib/generators/backbone/scaffold/templates/templates/show.jst
219
+ - lib/generators/backbone/scaffold/templates/views/edit_view.coffee
220
+ - lib/generators/backbone/scaffold/templates/views/index_view.coffee
221
+ - lib/generators/backbone/scaffold/templates/views/model_view.coffee
222
+ - lib/generators/backbone/scaffold/templates/views/new_view.coffee
223
+ - lib/generators/backbone/scaffold/templates/views/show_view.coffee
205
224
  - lib/railsy_backbone/version.rb
206
225
  - lib/railsy_backbone.rb
207
226
  - vendor/assets/javascripts/backbone.js