From 429549027f5442e4bd7e60b1a6debd8cf76cfaf0 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 9 May 2011 07:35:37 -0400 Subject: [PATCH] clean up mistakes --- bin/backbone-generator | 4 ++++ templates/collection_view.coffee.erb | 8 ++++---- templates/collection_view_spec.coffee.erb | 9 ++++----- templates/view.coffee.erb | 8 ++++---- templates/view_spec.coffee.erb | 6 +++--- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/bin/backbone-generator b/bin/backbone-generator index 5922c5e..437f1b3 100755 --- a/bin/backbone-generator +++ b/bin/backbone-generator @@ -33,6 +33,10 @@ class BackboneGenerator < Thor singularize(Thor::Util.snake_case(@name.gsub("::", "/"))) end + def css_class_name + underscore_name.gsub(%r{[^a-z0-9_]}, '-') + end + def plural_underscore_name pluralize(underscore_name) end diff --git a/templates/collection_view.coffee.erb b/templates/collection_view.coffee.erb index df2b724..a27919b 100644 --- a/templates/collection_view.coffee.erb +++ b/templates/collection_view.coffee.erb @@ -6,18 +6,18 @@ class window.<%= plural_object_name %>View extends Backbone.View initialize: -> @collection.bind('refresh', @addAll) - @render + this.render() @collection.fetch render: => - $(this.el).html(@template()) + $(this.el).html(this.template()) this addOne: (model) => view = new <%= object_name %>View({model: model}) this.$('.list').append(view.render().el) addAll: => - @collection.each(@addOne) + @collection.each(this.addOne()) addNew: => facility = new <%= object_name %>() @collection.add(facility) - @addOne(facility) + this.addOne(facility) diff --git a/templates/collection_view_spec.coffee.erb b/templates/collection_view_spec.coffee.erb index cc6c9b5..cd045bd 100644 --- a/templates/collection_view_spec.coffee.erb +++ b/templates/collection_view_spec.coffee.erb @@ -3,17 +3,16 @@ describe '<%= plural_object_name %>View', -> beforeEach -> collection = new <%= plural_object_name %>() + view = new <%= plural_object_name %>View({collection: collection}) + view.render() it 'should render', -> - view = new <%= plural_object_name %>View({collection: collectioon}) - view.render - expect($(view.el)).toContain('.list') - expect($(view.el)).toContail('button.new') + expect($(view.el)).toContain('button.new') it 'should add a new model when new is clicked', -> view.$('button.new').trigger('click') - expect(view.$('.list').toContain('.<%= underscore_name %>') + expect(view.$('.list')).toContain('.<%= css_class_name %>') expect(collection.length).toEqual(1) diff --git a/templates/view.coffee.erb b/templates/view.coffee.erb index 5065c42..59d55d6 100644 --- a/templates/view.coffee.erb +++ b/templates/view.coffee.erb @@ -5,17 +5,17 @@ class window.<%= object_name %>View extends Backbone.View } attributeFields: [] template: JST['<%= underscore_name %>s/view'] - className: '<%= underscore_name %>' + className: '<%= css_class_name %>' initialize: -> @model.bind('change', @render) @model.bind('remove', @remove) @model.view = this render: => - $(@el).html(@template(@model.toJSON)) - this.$('button.save').text(@model.isNew() ? 'Create' : 'Update') + $(@el).html(@template(@model.toJSON())) + this.$('button.save').text(if @model.isNew() then 'Create' else 'Update') this save: => - @model.save(@attributes) + @model.save(@attributes()) destroy: => if confirm("Are you sure?") @model.destroy diff --git a/templates/view_spec.coffee.erb b/templates/view_spec.coffee.erb index 16fb22b..1904641 100644 --- a/templates/view_spec.coffee.erb +++ b/templates/view_spec.coffee.erb @@ -7,7 +7,7 @@ describe '<%= object_name %>View', -> it 'should render with a create button', -> view = new <%= object_name %>View({model: model}) - view.render + view.render() expect(view.$('button.save')).toHaveText('Create') @@ -19,13 +19,13 @@ describe '<%= object_name %>View', -> view = new <%= object_name %>View({model: model}) it 'should render with an update button', -> - view.render + view.render() expect(view.$('button.save')).toHaveText('Update') it 'should destroy the model', -> spyOn(window, 'confirm').andReturn(true) - spyOn(model, 'destory') + spyOn(model, 'destroy') $('#container').append(view.el) view.$('button.delete').trigger('click')