From adf18ae11927256d8b1b84e63b358845776cedd9 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 23 Apr 2011 11:13:54 -0400 Subject: [PATCH] add app helper --- .autotest | 8 ++++++++ bin/backbone-generator | 5 +++++ spec/bin/backbone-generator_spec.rb | 8 ++++++++ templates/app_helper.js.erb | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 templates/app_helper.js.erb diff --git a/.autotest b/.autotest index 33dabb2..91a135e 100644 --- a/.autotest +++ b/.autotest @@ -2,4 +2,12 @@ Autotest.add_hook(:initialize) do |at| at.add_mapping(%r{bin/(.*)}, true) do |filename, matches| at.files_matching(%r{spec/bin/#{matches[1]}_spec\.rb}) end + + at.add_mapping(%r{spec/(.*)}, true) do |filename, matches| + at.files_matching(%r{#{filename}}) + end + + at.add_mapping(%r{templates/.*}, true) do |filename, matches| + at.files_matching(%r{spec/bin/.*_spec\.rb}) + end end diff --git a/bin/backbone-generator b/bin/backbone-generator index 5d4d426..e57a729 100755 --- a/bin/backbone-generator +++ b/bin/backbone-generator @@ -86,6 +86,11 @@ class BackboneGenerator < Thor template('spec_helper.js.erb', 'spec/javascripts/helpers/backbone_spec_helper.js') end + desc 'app-helper', "Generate an application helper for useful Backbone things" + def app_helper + template('app_helper.js.erb', 'public/javascripts/applications/backbone_helper.js') + end + private def pluralize(string) singularize(string) + 's' diff --git a/spec/bin/backbone-generator_spec.rb b/spec/bin/backbone-generator_spec.rb index 210613d..14cae1b 100644 --- a/spec/bin/backbone-generator_spec.rb +++ b/spec/bin/backbone-generator_spec.rb @@ -125,4 +125,12 @@ describe 'backbone-generator' do File.file?(collection = 'spec/javascripts/helpers/backbone_spec_helper.js').should be_true end end + + describe 'app helper' do + it "should generate an app helper" do + system %{bin/backbone-generator app-helper} + + File.file?(collection = 'public/javascripts/applications/backbone_helper.js').should be_true + end + end end diff --git a/templates/app_helper.js.erb b/templates/app_helper.js.erb new file mode 100644 index 0000000..130667c --- /dev/null +++ b/templates/app_helper.js.erb @@ -0,0 +1,19 @@ +/* Ensure a Collection is fetched and has entries before running a block of code. */ + +_.extend(Backbone.Collection.prototype, { + ensureFetched: function(callback) { + if (this.length == 0 || this._alreadyEnsureFetched) { + var _this = this; + var _refresher = function() { + _this.unbind('refresh', _refresher); + callback.apply(_this); + _this._alreadyEnsureFetched = true; + }; + this.bind('refresh', _refresher); + this.fetch(); + } else { + callback.apply(this); + } + } +}); +