Commit fb082e6b

mo <mo.khan@gmail.com>
2017-09-24 21:12:04
extract auto collection to setup js collections.
1 parent ca86a48
Changed files (5)
app/assets/javascripts/models/auto_collection.js.coffee
@@ -0,0 +1,6 @@
+class csx.AutoCollection
+  @install: (collectionName, json) ->
+    csx[collectionName] = @create(json)
+
+  @create: (json) ->
+    new Backbone.Collection(json)
app/controllers/my/kitchens_controller.rb
@@ -2,6 +2,7 @@ module My
   class KitchensController < BaseController
     def show
       @tags = Tag.unique_tags
+      @cakes = current_user.creations.includes(:category, :photos, :tags, :tag_taggings)
     end
   end
 end
app/views/application/json/_creations.jbuilder
@@ -0,0 +1,29 @@
+json.cache! items do
+  json.array! items do |cake|
+    json.id cake.id
+    json.name cake.name
+    json.slug cake.to_param
+    json.story cake.story
+    json.created_at cake.created_at
+    json.updated_at cake.updated_at
+    json.category do
+      json.id cake.category.id
+      json.name cake.category.name
+    end
+    json.user do
+      json.id cake.user.id
+      json.name cake.user.name
+    end
+    json.photos cake.photos do |photo|
+      json.id photo.id
+      json.large_url "https:#{photo.url_for(:large)}"
+      json.thumb_url "https:#{photo.url_for(:thumb)}"
+      json.created_at photo.created_at
+      json.updated_at photo.updated_at
+    end
+    json.tags cake.tags do |tag|
+      json.id tag.id
+      json.name tag.name
+    end
+  end
+end
app/views/application/_backbone_collection.html.erb
@@ -1,5 +1,5 @@
 <% cache items do %>
   <%= javascript_tag do %>
-    csx.<%= collection_name %> = new Backbone.Collection(<%= raw render partial: "application/json/#{partial_name}", locals: { items: items } %>);
+    csx.AutoCollection.install('<%= collection_name %>', <%= raw render partial: "application/json/#{partial_name}", locals: { items: items } %>);
   <% end %>
 <% end %>
app/views/my/kitchens/show.html.erb
@@ -1,11 +1,4 @@
 <% provide(:title, "Kitchen") -%>
-<%= content_for :javascript do -%>
-  <%= backbone_collection_for @categories %>
-  <%= backbone_collection_for @tags %>
-  <%= javascript_tag do %>
-    csx.initialize({ access_token: '<%= current_user.authentication_token %>' });
-  <% end %>
-<% end %>
 
 <div class="row">
   <div class="col-3">
@@ -14,3 +7,10 @@
   <div id="backbone-content" class="col-9"></div>
 </div>
 <div id="modal" class="modal hide fade"></div>
+
+<%= backbone_collection_for @categories %>
+<%= backbone_collection_for @tags %>
+
+<%= javascript_tag do %>
+  csx.initialize({ access_token: '<%= current_user.authentication_token %>' });
+<% end %>