Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for (decaffeinated) JavaScript? #121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/generators/backbone/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class InstallGenerator < Rails::Generators::Base

class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
:desc => "Skip Git ignores and keeps"

def inject_backbone
inject_into_file "app/assets/javascripts/application.js", :before => "//= require_tree" do
"//= require underscore\n//= require backbone\n//= require backbone_rails_sync\n//= require backbone_datalink\n//= require backbone/#{application_name.underscore}\n"
Expand All @@ -26,9 +26,9 @@ def create_dir_layout
end

def create_app_file
template "app.coffee", "app/assets/javascripts/backbone/#{application_name.underscore}.js.coffee"
template "app.#{asset_suffix}", "app/assets/javascripts/backbone/#{application_name.underscore}.#{asset_suffix}"
end

end
end
end
end
10 changes: 10 additions & 0 deletions lib/generators/backbone/install/templates/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {

window.<%= js_app_name %> = {
Models: {},
Collections: {},
Routers: {},
Views: {}
};

}).call(this);
9 changes: 5 additions & 4 deletions lib/generators/backbone/model/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class ModelGenerator < Rails::Generators::NamedBase

source_root File.expand_path("../templates", __FILE__)
desc "This generator creates a backbone model"

argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"

def create_backbone_model
template "model.coffee", "#{backbone_path}/models/#{file_name}.js.coffee"
template "model.#{asset_suffix}",
"#{backbone_path}/models/#{file_name}.#{asset_suffix}"
end

end
end
end
end
20 changes: 20 additions & 0 deletions lib/generators/backbone/model/templates/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function() {

<%= model_namespace %> = Backbone.Model.extend({

paramRoot: '<%= singular_table_name %>',

defaults: {
<% attributes.each_with_index do |attribute,i| -%>
<%= attribute.name %>: null <%= i < (attributes.size - 1) ? "," : "" %>
<% end -%>
}

});

<%= collection_namespace %>Collection = Backbone.Collection.extend({
model: <%= model_namespace %>,
url: '<%= route_url %>'
});

}).call(this);
18 changes: 18 additions & 0 deletions lib/generators/backbone/resource_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def application_name
def uncapitalize(str)
str[0, 1].downcase << str[1..-1]
end

def asset_suffix
case Rails.configuration.generators.rails[:javascript_engine]
when :js
"js"
else
"js.coffee"
end
end

def template_suffix
case Rails.configuration.generators.rails[:javascript_templates]
when :hbs
"js.jst.hbs"
else
"jst.ejs"
end
end

end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/generators/backbone/router/router_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RouterGenerator < Rails::Generators::NamedBase

source_root File.expand_path("../templates", __FILE__)
desc "This generator creates a backbone router with views and templates for the provided actions"

argument :actions, :type => :array, :default => [], :banner => "action action"

RESERVED_JS_WORDS = %W{
Expand All @@ -25,20 +25,20 @@ def validate_no_reserved_words
end

def create_router_files
template 'router.coffee', File.join(backbone_path, "routers", class_path, "#{file_name}_router.js.coffee")
template "router.#{asset_suffix}", File.join(backbone_path, "routers", class_path, "#{file_name}_router.#{asset_suffix}")
end

def create_view_files
actions.each do |action|
@action = action
@view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.js.coffee")
@jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.jst.ejs")
@view_path = File.join(backbone_path, "views", plural_name, "#{action}_view.#{asset_suffix}")
@jst_path = File.join(backbone_path,"templates", plural_name, "#{action}.#{template_suffix}")

template "view.coffee", @view_path
template "template.jst", @jst_path
template "view.#{asset_suffix}", @view_path
template "template.#{template_suffix}", @jst_path
end
end

end
end
end
end
22 changes: 22 additions & 0 deletions lib/generators/backbone/router/templates/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(function() {

<%= router_namespace %>Router = Backbone.Router.extend({

initialize: function(options) {},

routes: {
<% actions.each_with_index do |action,i| -%>
"<%= action %>": "<%= action %>"<%= i < action.size - 1 ? "," : "" %>
<% end -%>
},

<% actions.each_with_index do |action,i| -%>
<%= action %>: function() {
this.view = new <%= "#{view_namespace}.#{action.camelize}View()" %>;
$("#<%= plural_name %>").html(this.view.render().el);
}<%= i < action.size - 1 ? "," : "" %>
<% end -%>

});

}).call(this);
2 changes: 2 additions & 0 deletions lib/generators/backbone/router/templates/template.jst.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1><%= class_name %>#<%= @action %></h1>
<p>Find me in <%= @jst_path %></p>
16 changes: 16 additions & 0 deletions lib/generators/backbone/router/templates/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(function() {

(<%= view_namespace %>) || (<%= view_namespace %> = {});

<%= view_namespace %>.<%= @action.camelize %>View = Backbone.View.extend({

template: JST["<%= jst @action %>"],

render: function() {
$(this.el).html(this.template());
return this;
}

});

}).call(this);
12 changes: 6 additions & 6 deletions lib/generators/backbone/scaffold/scaffold_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class ScaffoldGenerator < ModelGenerator
desc "This generator creates the client side crud scaffolding"

def create_router_files
template 'router.coffee', File.join(backbone_path, "routers", class_path, "#{plural_name}_router.js.coffee")
template "router.#{asset_suffix}", File.join(backbone_path, "routers", class_path, "#{plural_name}_router.#{asset_suffix}")
end

def create_view_files
available_views.each do |view|
template "views/#{view}_view.coffee", File.join(backbone_path, "views", plural_name, "#{view}_view.js.coffee")
template "templates/#{view}.jst", File.join(backbone_path, "templates", plural_name, "#{view}.jst.ejs")
template "views/#{view}_view.#{asset_suffix}", File.join(backbone_path, "views", plural_name, "#{view}_view.#{asset_suffix}")
template "templates/#{view}.#{template_suffix}", File.join(backbone_path, "templates", plural_name, "#{view}.#{template_suffix}")
end

template "views/model_view.coffee", File.join(backbone_path, "views", plural_name, "#{singular_name}_view.js.coffee")
template "templates/model.jst", File.join(backbone_path, "templates", plural_name, "#{singular_name}.jst.ejs")
template "views/model_view.#{asset_suffix}", File.join(backbone_path, "views", plural_name, "#{singular_name}_view.#{asset_suffix}")
template "templates/model.#{template_suffix}", File.join(backbone_path, "templates", plural_name, "#{singular_name}.#{template_suffix}")
end

protected
Expand All @@ -28,4 +28,4 @@ def available_views

end
end
end
end
20 changes: 20 additions & 0 deletions lib/generators/backbone/scaffold/templates/model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function() {

<%= model_namespace %> = Backbone.Model.extend({

paramRoot: '<%= singular_table_name %>',

defaults: {
<% attributes.each_with_index do |attribute,i| -%>
<%= attribute.name %>: null <%= i < (attributes.size - 1) ? "," : "" %>
<% end -%>
}

});

<%= collection_namespace %>Collection = Backbone.Collection.extend({
model: <%= model_namespace %>,
url: '<%= route_url %>'
});

}).call(this);
42 changes: 42 additions & 0 deletions lib/generators/backbone/scaffold/templates/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(function() {

<%= router_namespace %>Router = Backbone.Router.extend({

initialize: function(options) {
this.<%= plural_model_name %> = new <%= collection_namespace %>Collection();
this.<%= plural_model_name %>.reset(options.<%= plural_model_name %>);
},

routes: {
"new" : "new<%= class_name %>",
"index" : "index",
":id/edit" : "edit",
":id" : "show",
".*" : "index"
},

new<%= class_name %>: function() {
this.view = new <%= "#{view_namespace}.NewView({ collection: this.#{plural_name} })" %>;
$("#<%= plural_name %>").html(this.view.render().el);
},

index: function() {
this.view = new <%= "#{view_namespace}.IndexView({ #{plural_name}: this.#{plural_name} })" %>;
$("#<%= plural_name %>").html(this.view.render().el);
},

show: function(id) {
var <%= singular_name %> = this.<%= plural_name %>.get(id);
this.view = new <%= "#{view_namespace}.ShowView({ model: #{singular_name} })" %>;
$("#<%= plural_name %>").html(this.view.render().el);
},

edit: function(id) {
var <%= singular_name %> = this.<%= plural_name %>.get(id);
this.view = new <%= "#{view_namespace}.EditView({ model: #{singular_name} })" %>;
$("#<%= plural_name %>").html(this.view.render().el);
}

});

}).call(this);
17 changes: 17 additions & 0 deletions lib/generators/backbone/scaffold/templates/templates/edit.jst.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Edit <%= singular_table_name %></h1>

<form id="edit-<%= singular_table_name %>" name="<%= singular_table_name %>">
<% attributes.each do |attribute| -%>
<div class="field">
<label for="<%= attribute.name %>"> <%= attribute.name %>:</label>
<input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="{{ <%= attribute.name %> }}" >
</div>

<% end -%>
<div class="actions">
<input type="submit" value="Update <%= human_name %>" />
</div>

</form>

<a href="#/index">Back</a>
16 changes: 16 additions & 0 deletions lib/generators/backbone/scaffold/templates/templates/index.jst.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>Listing <%= plural_table_name %></h1>

<table id="<%= plural_name %>-table">
<tr>
<% attributes.each do |attribute| -%>
<th><%= attribute.human_name %></th>
<% end -%>
<th></th>
<th></th>
<th></th>
</tr>
</table>

<br/>

<a href="#/new">New <%= human_name %></a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% attributes.each do |attribute| -%>
<td>{{ <%= attribute.name %> }}</td>
<% end -%>

<td><a href="#/{{ id }}">Show</td>
<td><a href="#/{{ id }}/edit">Edit</td>
<td><a href="#/{{ id }}/destroy" class="destroy">Destroy</a></td>
17 changes: 17 additions & 0 deletions lib/generators/backbone/scaffold/templates/templates/new.jst.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>New <%= singular_table_name %></h1>

<form id="new-<%= singular_table_name %>" name="<%= singular_table_name %>">
<% attributes.each do |attribute| -%>
<div class="field">
<label for="<%= attribute.name %>"> <%= attribute.name %>:</label>
<input type="text" name="<%= attribute.name %>" id="<%= attribute.name %>" value="{{ <%= attribute.name %> }}" >
</div>

<% end -%>
<div class="actions">
<input type="submit" value="Create <%= human_name %>" />
</div>

</form>

<a href="#/index">Back</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% attributes.each do |attribute| -%>
<p>
<b><%= attribute.human_name %>:</b>
{{ <%= attribute.name %> }}
</p>

<% end -%>

<a href="#/index">Back</a>
34 changes: 34 additions & 0 deletions lib/generators/backbone/scaffold/templates/views/edit_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function() {

(<%= view_namespace %>) || (<%= view_namespace %> = {});

<%= view_namespace %>.EditView = Backbone.View.extend({

template : JST["<%= jst 'edit' %>"],

events : {
"submit #edit-<%= singular_name %>" : "update"
},

update : function(e) {
e.preventDefault();
e.stopPropagation();

var view = this;
this.model.save(null, {
success : function(<%= singular_name %>) {
view.model = <%= singular_name %>;
window.location.hash = "/" + view.model.id;
}
});
},

render : function() {
this.$el.html(this.template(this.model.toJSON()));
this.$("form").backboneLink(this.model);
return this;
}

});

}).call(this);
Loading