如何使用Backbone.js将参数传递给控制器​​?

时间:2012-07-06 05:52:02

标签: ruby-on-rails backbone.js

在我的一个backbone.js视图中,我有以下代码:

  events:
    'click .filter_classroom_size': 'filterClassroom'

  filterClassroom: (event)->
    event.stopPropagation()
    event.preventDefault()
    Backbone.history.navigate("classrooms?nationality", true)

我的路由器有以下代码:

  routes:
    'classrooms?:filter': 'classrooms'

  initialize: ->
    @collection = new Classrooms.Collections.Entries()
    @collection.fetch()

  classrooms: (filter) ->
    classroomView = new Seats.Views.ClassroomIndex(collection: @collection)
    $('#output_container').html(classroomView.render().el)
    alert("#{filter}") # this prints out the filter parameter

classrooms_controller.rb:

class ClassroomsController < ApiController
  def show
    # How do I send a parameter here so I can filter on it?
    respond_with Classroom.all
  end
end

我希望能够将参数传递给控制器​​,以便我可以过滤数据库结果。

谢谢!

1 个答案:

答案 0 :(得分:0)

Backbone使用jQuery.ajax,因此您只需在savefetchdestroy等方法中传递jQuery.ajax选项。

initialize: ->
  @collection = new Classrooms.Collections.Entries()
  @collection.fetch
    data:
      your_key: your_value