add app helper

This commit is contained in:
John Bintz 2011-04-23 11:13:54 -04:00
parent 05ff5d3def
commit adf18ae119
4 changed files with 40 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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