diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index 87d466f8f..89ec70313 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -1,8 +1,9 @@ <% + require_asset ("./main_include.js") -# Include plugin javascripts -DiscoursePluginRegistry.javascripts.each do |js| - require_asset(js) -end +# Include plugin javascripts/handlebars templates +DiscoursePluginRegistry.javascripts.each { |js| require_asset(js) } +DiscoursePluginRegistry.handlebars.each { |hb| require_asset(hb) } + %> diff --git a/app/assets/javascripts/discourse/ember/resolver.js b/app/assets/javascripts/discourse/ember/resolver.js index ada682f4e..f2f8dc848 100644 --- a/app/assets/javascripts/discourse/ember/resolver.js +++ b/app/assets/javascripts/discourse/ember/resolver.js @@ -16,7 +16,15 @@ Discourse.Resolver = Ember.DefaultResolver.extend({ @returns {Template} the template (if found) **/ resolveTemplate: function(parsedName) { - return this.findMobileTemplate(parsedName) || this.findTemplate(parsedName) || Ember.TEMPLATES.not_found; + return this.findPluginTemplate(parsedName) || + this.findMobileTemplate(parsedName) || + this.findTemplate(parsedName) || + Ember.TEMPLATES.not_found; + }, + + findPluginTemplate: function(parsedName) { + var pluginParsedName = this.parseName(parsedName.fullName.replace("template:", "template:javascripts/")); + return this.findTemplate(pluginParsedName); }, findMobileTemplate: function(parsedName) { @@ -49,4 +57,4 @@ Discourse.Resolver = Ember.DefaultResolver.extend({ } } -}); \ No newline at end of file +}); diff --git a/app/assets/javascripts/main_include.js b/app/assets/javascripts/main_include.js index b1b26036f..71a6997e5 100644 --- a/app/assets/javascripts/main_include.js +++ b/app/assets/javascripts/main_include.js @@ -36,3 +36,4 @@ //= require_tree ./discourse/templates //= require_tree ./discourse/routes //= require_tree ./discourse/initializers + diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index c5266664e..f635e6347 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -7,6 +7,7 @@ class DiscoursePluginRegistry attr_accessor :javascripts attr_accessor :server_side_javascripts attr_accessor :stylesheets + attr_accessor :handlebars # Default accessor values def javascripts @@ -20,6 +21,10 @@ class DiscoursePluginRegistry def stylesheets @stylesheets ||= Set.new end + + def handlebars + @handlebars ||= Set.new + end end def register_js(filename, options={}) @@ -48,10 +53,15 @@ class DiscoursePluginRegistry self.class.stylesheets end + def handlebars + self.class.handlebars + end + def self.clear self.javascripts = nil self.server_side_javascripts = nil self.stylesheets = nil + self.handlebars = nil end def self.setup(plugin_class) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index bd741f2bf..17f865c47 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -93,7 +93,6 @@ class Plugin::Instance @javascripts << js end - def register_asset(file,opts=nil) full_path = File.dirname(path) << "/assets/" << file assets << full_path @@ -154,10 +153,12 @@ class Plugin::Instance end unless assets.blank? assets.each do |asset| - if asset =~ /\.js$|.js.erb$/ + if asset =~ /\.js$|\.js\.erb$/ DiscoursePluginRegistry.javascripts << asset elsif asset =~ /\.css$|\.scss$/ DiscoursePluginRegistry.stylesheets << asset + elsif asset =~ /\.js\.handlebars$$/ + DiscoursePluginRegistry.handlebars << asset end end diff --git a/test/javascripts/ember/resolver_test.js b/test/javascripts/ember/resolver_test.js index f58a3db93..fc011aa20 100644 --- a/test/javascripts/ember/resolver_test.js +++ b/test/javascripts/ember/resolver_test.js @@ -5,7 +5,7 @@ var lookup = function(lookupString, expectedTemplate, message) { equal(Discourse.__container__.lookup(lookupString, {singleton: false}), expectedTemplate, message); }; -var setTemplates = function(lookupStrings) { +var setTemplates = function(lookupStrings) { lookupStrings.forEach(function(lookupString) { Ember.TEMPLATES[lookupString] = lookupString; }); @@ -95,6 +95,19 @@ test("resolves mobile templates to 'mobile/' namespace", function() { lookup("template:baz", "baz", "falling back to a normal version when mobile version is not present"); }); +test("resolves plugin templates to 'javascripts/' namespace", function() { + setTemplates([ + "javascripts/foo", + "bar", + "javascripts/bar", + "baz" + ]); + + lookup("template:foo", "javascripts/foo", "finding plugin version even if normal one is not present"); + lookup("template:bar", "javascripts/bar", "preferring plugin version when both versions are present"); + lookup("template:baz", "baz", "falling back to a normal version when plugin version is not present"); +}); + test("resolves templates with 'admin' prefix to 'admin/templates/' namespace", function() { setTemplates([ "admin/templates/foo",