little bit of cleanup, add collection view

This commit is contained in:
John Bintz 2011-04-15 15:32:04 -04:00
parent 6469fd7fa7
commit 81a31a66df
6 changed files with 74 additions and 14 deletions

View File

@ -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

View File

@ -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

View File

@ -1,3 +1,4 @@
var <%= object_name %>sCollection = Backbone.Collection.extend({
url: '/<%= underscore_name %>'
url: '/<%= underscore_name %>',
model: <%= object_name %>
});

View File

@ -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);
}
});

View File

@ -0,0 +1 @@
<div class="list"></div>

View File

@ -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');
});
});