From 81a31a66df3cc71c8a606cf8cb3b5b139493d4a4 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 15 Apr 2011 15:32:04 -0400 Subject: [PATCH] little bit of cleanup, add collection view --- bin/backbone-generator | 8 ++++++ spec/bin/backbone-generator_spec.rb | 40 ++++++++++++++++++--------- templates/collection.js.erb | 3 +- templates/collection_view.js.erb | 22 +++++++++++++++ templates/collection_view.jst.erb | 1 + templates/collection_view_spec.js.erb | 14 ++++++++++ 6 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 templates/collection_view.js.erb create mode 100644 templates/collection_view.jst.erb create mode 100644 templates/collection_view_spec.js.erb diff --git a/bin/backbone-generator b/bin/backbone-generator index 3377a34..d5a4117 100755 --- a/bin/backbone-generator +++ b/bin/backbone-generator @@ -42,6 +42,14 @@ class BackboneGenerator < Thor template('collection.js.erb', "public/javascripts/collections/#{underscore_name}s.js") template('collection_spec.js.erb', "spec/javascripts/collections/#{underscore_name}s_spec.js") end + + desc 'collection-view Namespaced::Name', "Create a collection view" + def collection_view(name) + @name = name + template('collection_view.js.erb', "public/javascripts/views/#{underscore_name}s.js") + template('collection_view.jst.erb', "app/views/#{underscore_name}s.jst") + template('collection_view_spec.js.erb', "spec/javascripts/views/#{underscore_name}s_spec.js") + end end BackboneGenerator.start diff --git a/spec/bin/backbone-generator_spec.rb b/spec/bin/backbone-generator_spec.rb index 8aa1d0c..196c89c 100644 --- a/spec/bin/backbone-generator_spec.rb +++ b/spec/bin/backbone-generator_spec.rb @@ -25,28 +25,42 @@ describe 'backbone-generator' do describe 'view' do it "should generate the view files" do - system %{bin/backbone-generator view Section::View} + system %{bin/backbone-generator view Section::Model} - File.file?(view = 'public/javascripts/views/section/view.js').should be_true - File.file?(spec = 'spec/javascripts/views/section/view_spec.js').should be_true - File.file?(template = 'app/views/section/view.jst').should be_true + File.file?(view = 'public/javascripts/views/section/model.js').should be_true + File.file?(spec = 'spec/javascripts/views/section/model_spec.js').should be_true + File.file?(template = 'app/views/section/model.jst').should be_true - File.read(view).should match(/SectionView/) - File.read(view).should match(%r{template: JST\['section/view'\]}) - File.read(spec).should match(/SectionView/) + File.read(view).should match(/SectionModel/) + File.read(view).should match(%r{template: JST\['section/model'\]}) + File.read(spec).should match(/SectionModel/) + end + end + + describe 'collection view' do + it "should generate the collection view files" do + system %{bin/backbone-generator collection-view Section::Model} + + File.file?(view = 'public/javascripts/views/section/models.js').should be_true + File.file?(spec = 'spec/javascripts/views/section/models_spec.js').should be_true + File.file?(template = 'app/views/section/models.jst').should be_true + + File.read(view).should match(/SectionModels/) + File.read(view).should match(%r{template: JST\['section/models'\]}) + File.read(spec).should match(/SectionModels/) end end describe 'collection' do it "should generate the collection files" do - system %{bin/backbone-generator collection Section::Collection} + system %{bin/backbone-generator collection Section::Model} - File.file?(collection = 'public/javascripts/collections/section/collections.js').should be_true - File.file?(spec = 'spec/javascripts/collections/section/collections_spec.js').should be_true + File.file?(collection = 'public/javascripts/collections/section/models.js').should be_true + File.file?(spec = 'spec/javascripts/collections/section/models_spec.js').should be_true - File.read(collection).should match(/SectionCollectionsCollection/) - File.read(collection).should match(%r{section/collection}) - File.read(spec).should match(/SectionCollectionsCollection/) + File.read(collection).should match(/SectionModelsCollection/) + File.read(collection).should match(%r{section/model}) + File.read(spec).should match(/SectionModelsCollection/) end end end diff --git a/templates/collection.js.erb b/templates/collection.js.erb index a6fc771..a169271 100644 --- a/templates/collection.js.erb +++ b/templates/collection.js.erb @@ -1,3 +1,4 @@ var <%= object_name %>sCollection = Backbone.Collection.extend({ - url: '/<%= underscore_name %>' + url: '/<%= underscore_name %>', + model: <%= object_name %> }); diff --git a/templates/collection_view.js.erb b/templates/collection_view.js.erb new file mode 100644 index 0000000..1a78172 --- /dev/null +++ b/templates/collection_view.js.erb @@ -0,0 +1,22 @@ +var <%= object_name %>sView = Backbone.View.extend({ + template: JST['<%= underscore_name %>s'], + initialize: function(collection) { + _.bindAll(this, 'render', 'addOne', 'addAll'); + + this.collection = collection; + this.collection.bind('refresh', this.addAll); + + this.render(); + this.collection.fetch() + }, + render: function() { + $(this.el).html(this.template()); + }, + addOne: function(model) { + var view = new <%= object_name %>({model: model}); + this.$('.list').append(view.render().el); + }, + addAll: function() { + this.collection.each(this.addOne); + } +}); diff --git a/templates/collection_view.jst.erb b/templates/collection_view.jst.erb new file mode 100644 index 0000000..4882ccc --- /dev/null +++ b/templates/collection_view.jst.erb @@ -0,0 +1 @@ +
diff --git a/templates/collection_view_spec.js.erb b/templates/collection_view_spec.js.erb new file mode 100644 index 0000000..5469bf6 --- /dev/null +++ b/templates/collection_view_spec.js.erb @@ -0,0 +1,14 @@ +describe('<%= object_name %>sView', function() { + var view, collection; + + beforeEach(function() { + collection = new <%= object_name %>s(); + }); + + it('should render', function() { + view = new <%= object_name %>sView(collection); + view.render(); + + expect($(view.el)).toContain('.list'); + }); +});