diff options
| author | mo <mo.khan@gmail.com> | 2017-09-24 16:41:58 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2017-09-24 16:41:58 -0600 |
| commit | 576a045848457252df3b96e1bd66532d5c86b382 (patch) | |
| tree | 6a1f669d9e843ff5eb37a5370b2db0d6979f7e9c | |
| parent | 79f42163ba1d6c5b699f9f8281aaab8a2a059847 (diff) | |
bind auto collections to a url.
| -rw-r--r-- | app/assets/javascripts/models/auto_collection.js.coffee | 14 | ||||
| -rw-r--r-- | app/helpers/application_helper.rb | 4 | ||||
| -rw-r--r-- | app/views/application/_backbone_collection.html.erb | 2 | ||||
| -rw-r--r-- | spec/javascripts/models/auto_collection_spec.js.coffee | 15 |
4 files changed, 19 insertions, 16 deletions
diff --git a/app/assets/javascripts/models/auto_collection.js.coffee b/app/assets/javascripts/models/auto_collection.js.coffee index 796c1872..335e9ad2 100644 --- a/app/assets/javascripts/models/auto_collection.js.coffee +++ b/app/assets/javascripts/models/auto_collection.js.coffee @@ -1,6 +1,10 @@ class csx.AutoCollection - @install: (collectionName, json) -> - csx.Collections[collectionName] = @create(json) - - @create: (json) -> - new Backbone.Collection(json) + @install: (modelName, pluralName, json) -> + model = csx.Models[modelName] + if _.isUndefined(model) + csx.Collections[modelName] = new Backbone.Collection(json) + else + collection = Backbone.Collection.extend + model: model + url: "/api/v1/#{pluralName}" + csx.Collections[modelName] = new collection(json) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index d1891af6..74a57170 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -41,9 +41,9 @@ module ApplicationHelper def backbone_collection_for(items) render partial: 'backbone_collection', locals: { - collection_name: items.model_name.human, items: items, - partial_name: items.model_name.plural, + model_name: items.model_name.human, + plural_name: items.model_name.plural, } end end diff --git a/app/views/application/_backbone_collection.html.erb b/app/views/application/_backbone_collection.html.erb index 38ffb2cb..5dcb4951 100644 --- a/app/views/application/_backbone_collection.html.erb +++ b/app/views/application/_backbone_collection.html.erb @@ -1,5 +1,5 @@ <% cache items do %> <%= javascript_tag do %> - csx.AutoCollection.install('<%= collection_name %>', <%= raw render partial: "application/json/#{partial_name}", locals: { items: items } %>); + csx.AutoCollection.install('<%= model_name %>', '<%= plural_name %>', <%= raw render partial: "application/json/#{plural_name}", locals: { items: items } %>); <% end %> <% end %> diff --git a/spec/javascripts/models/auto_collection_spec.js.coffee b/spec/javascripts/models/auto_collection_spec.js.coffee index 18241774..30f073ce 100644 --- a/spec/javascripts/models/auto_collection_spec.js.coffee +++ b/spec/javascripts/models/auto_collection_spec.js.coffee @@ -5,7 +5,7 @@ describe "AutoCollection", -> describe ".install", -> it "installs a categories collection", -> - subject.install('Category', [id: 1, name: 'cakes']) + subject.install('Category', 'categories', [id: 1, name: 'cakes']) collection = csx.Collections.Category expect(collection).not.toBe(undefined) @@ -13,15 +13,14 @@ describe "AutoCollection", -> expect(collection.first().get('name')).toEqual('cakes') it "binds the proper model", -> - subject.install('Category', []) + subject.install('Category', 'categories', []) expect(csx.Collections.Category.model).toEqual(csx.Models.Category) it "binds the proper url", -> - subject.install('Cake', []) + subject.install('Cake', 'cakes', []) expect(csx.Collections.Cake.url).toEqual('/api/v1/cakes') - -#class csx.Collections.CakesCollection extends Backbone.Collection - #model: csx.Models.Cake - #url: '/api/v1/cakes' - + it "works for models that do not map to a backbone model", -> + subject.install('Tag', 'acts_as_taggable_on_tags', []) + expect(csx.Collections.Tag.model).toEqual(Backbone.Model) + expect(csx.Collections.Tag.url).toEqual(undefined) |
