在觀看Ryan Bates Railscast之後嘗試拾取Backbone,我不知道如何從我的Rails控制器傳遞多個實例變量並在我的Backbone視圖中渲染它們。任何幫助將不勝感激。我試過類似的東西:將多個對象傳遞給Rails 4中的骨幹視圖
respond_with({ photos: @photos, feature: @feature, only: @categories })
但我不知道如何在我的Backbone視圖中傳遞它們。
class PhotosController < ApplicationController
respond_to :html, :json
def index
photo = Five00px::Photo.new("jfvYEpvJLv06t0blEKNPuJyU3vjqQP6vnXh6KX3O")
options = {
consumer_key: photo.consumer.key,
feature: params["feature"] ? params["feature"] : "popular",
image_size: 2 }
@photos = photo.photo_stream(options).photos
@feature = photo.stream_feature
@categories = photo.stream_categories
respond_with @photos
end
end
路由器:
class Five00.Routers.Photos extends Backbone.Router
routes:
"" : "index"
initialize: ->
@collection = new Five00.Collections.Photos()
@collection.fetch()
index: ->
photo_view = new Five00.Views.PhotosIndex(collection: @collection)
$("#photos").html(photo_view.render().el)
查看:
class Five00.Views.PhotosIndex extends Backbone.View
template: JST['photos/index']
initialize: ->
@collection.on("sync", @render, this)
render: ->
$(@el).html(@template(photos: @collection))
this