Commit 0a40d930

mo <mo.khan@gmail.com>
2017-10-07 21:33:33
load all collections using backbone_collection_for helper.
1 parent 6e36c75
Changed files (7)
app
assets
controllers
views
application
my
app/assets/javascripts/controllers/my/cakes_controller.js.coffee
@@ -2,7 +2,7 @@ class csx.Controllers.My.CakesController extends Marionette.Controller
   views: csx.Views.My.Cakes
   initialize: (options) ->
     @content_region = csx.Application.content_region
-    @cakes = csx.Application.request('CakesRepository')
+    @cakes = csx.Collections.Creation
 
   index: ->
     @selectTab()
app/assets/javascripts/models/cake.js.coffee
@@ -14,7 +14,8 @@ class csx.Models.Cake extends Backbone.Model
     return "Category can't be blank" unless attributes.category_id
 
   photos: ->
-    csx.Application.request('PhotosRepository', @id)
+    cake_id = @id
+    new Backbone.Collection(csx.Collections.Photo.filter((photo) => photo.get("cake_id") == cake_id))
 
   public_url: ->
     "#{window.location.origin}/creations/#{@get('slug')}"
@@ -25,3 +26,5 @@ class csx.Models.Cake extends Backbone.Model
 class csx.Collections.CakesCollection extends Backbone.Collection
   model: csx.Models.Cake
   url: '/api/v1/cakes'
+
+csx.Models.Creation = csx.Models.Cake
app/assets/javascripts/views/my/photos/new_modal_view.js.coffee
@@ -11,7 +11,7 @@ class csx.Views.My.Photos.NewModalView extends Marionette.ItemView
 
   constructor: (options) ->
     super(options)
-    @collection = csx.Application.request('PhotosRepository', options.cake.id)
+    @collection = csx.Collections.Photo
     @cake = options.cake
     @model = new @collection.model(cake_id: @cake.id)
 
app/assets/javascripts/cakeside.js.coffee
@@ -21,36 +21,13 @@ window.csx =
         xhr.setRequestHeader "Authorization", "Token token=#{data.access_token}"
 
     csx.Application = new Marionette.Application()
-    csx.Application.addRegions
-      content_region: '#backbone-content'
-
+    csx.Application.addRegions(content_region: '#backbone-content')
     csx.Application.addInitializer (options) ->
-      new csx.Routers.CakesRouter
-        controller: new csx.Controllers.My.CakesController()
-      new csx.Routers.TutorialsRouter
-        controller: new csx.Controllers.My.TutorialsController()
-      new csx.Routers.DashboardRouter
-        controller: new csx.Controllers.My.DashboardController()
-      new csx.Routers.ProfileRouter
-        controller: new csx.Controllers.My.ProfileController()
-
+      new csx.Routers.CakesRouter(controller: new csx.Controllers.My.CakesController())
+      new csx.Routers.TutorialsRouter(controller: new csx.Controllers.My.TutorialsController())
+      new csx.Routers.DashboardRouter(controller: new csx.Controllers.My.DashboardController())
+      new csx.Routers.ProfileRouter(controller: new csx.Controllers.My.ProfileController())
     csx.Application.on 'start', ->
       if Backbone.history
         Backbone.history.start()
-
-    @cakes = new csx.Collections.CakesCollection()
-
-    csx.Application.reqres.setHandler 'CakesRepository', =>
-      @cakes
-    @photos_cache = {}
-    csx.Application.reqres.setHandler 'PhotosRepository', (cake_id) =>
-      if @photos_cache[cake_id]
-        @photos_cache[cake_id]
-      else
-        photos = new csx.Collections.PhotosCollection(cake_id: cake_id)
-        @photos_cache[cake_id] = photos
-        photos.fetch(reset: true)
-        photos
-
-    @cakes.fetch(reset: true).done ->
-      csx.Application.start()
+    csx.Application.start()
app/controllers/my/kitchens_controller.rb
@@ -2,8 +2,9 @@ module My
   class KitchensController < BaseController
     def show
       @tags = Tag.unique_tags
-      #@cakes = current_user.creations.includes(:category, :photos, :tags, :tag_taggings)
+      @cakes = current_user.creations.includes(:category, :photos, :tags, :tag_taggings)
       @tutorials = current_user.tutorials.includes(:tags, :tag_taggings)
+      @photos = Photo.where(imageable: @cakes)
     end
   end
 end
app/views/application/json/_photos.jbuilder
@@ -0,0 +1,13 @@
+json.array! @photos do |photo|
+  json.cache! ['v1', photo] do
+    json.id photo.id
+    json.cake_id photo.imageable_id
+    json.content_type photo.content_type
+    json.original_filename photo.original_filename
+    json.thumb_url photo.url_for(:thumb)
+    json.large_url photo.url_for(:large)
+    json.original_url photo.url_for(:original)
+    json.created_at photo.created_at
+    json.updated_at photo.updated_at
+  end
+end
app/views/my/kitchens/show.html.erb
@@ -10,7 +10,9 @@
 
 <%= backbone_collection_for @categories %>
 <%= backbone_collection_for @tags %>
+<%= backbone_collection_for @cakes %>
 <%= backbone_collection_for @tutorials %>
+<%= backbone_collection_for @photos %>
 
 <%= javascript_tag do %>
   csx.initialize({ access_token: '<%= current_user.authentication_token %>' });