在我的骨干视图中,我在这样的集合上调用fetch:
@collection.fetch
data:
query: query
success: (collection, response) =>
#do stuff
如果我只想在我的rails路由中调用spot #index,这非常有用。但是我想在我的spots_controller.rb中设置一个“搜索”路径,它返回一组不同的结果。我该如何设置呢。
这是我的模特和收藏品:
class App.Models.Spot extends Backbone.Model
initialize: ->
url: ->
if @isNew()
"#{App.getURL()}/spots.json"
else
"#{App.getURL()}/spots/#{@id}.json"
urlRoot: ->
App.getURL() + "/spots.json"
class App.Collections.SpotsCollection extends Backbone.Collection
model: App.Models.Spot
initialize: (models, options) ->
url: ->
"#{App.getURL()}/spots.json"
这是一个地点的路由器:
class App.Routers.SpotsRouter extends Backbone.Router
routes:
'' : 'index'
initialize: ->
@collection = new App.Collections.SpotsCollection()
@collection.fetch()
index: ->
在我的rails后端我有一个简单的控制器,这里是spots_controller.rb
class SpotsController < ApplicationController
def index
@spots = Spot.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @spots }
end
end
end
我只是好奇我将如何添加搜索方法,所以当我调用fetch时,我可以告诉它根据一些搜索条件获取所有的现场记录。我知道我可以在调用我的spot #index action时添加一些数据参数,然后在索引动作中找到这些术语,但是,好像告诉fetch从spot #search获取记录会更好。做这个的最好方式是什么?谢谢!