如何使用Backbone.js将参数传递给控制器?
问题描述:
在我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
我想能够将参数传递到控制器,以便我可以过滤数据库结果。
谢谢!
答
骨干网使用jQuery.ajax引擎盖下,所以你可以简单地通过内部方法,如save
,fetch
和destroy
jQuery.ajax选项。
initialize: ->
@collection = new Classrooms.Collections.Entries()
@collection.fetch
data:
your_key: your_value
如何从视图发送参数到路由器放入'initialize'节?另外,我只是把你的例子放在初始化部分,并通过控制器中的'Rails.logger.info'输出参数,键/值没有显示。 – Goalie 2012-07-06 06:07:48
当你调用了教室功能时,你可以调用@ collection.fetch({data:{filter:filter}}),因为你有过滤变量。关于记录器,看看你的代码是否正确识别,它应该工作。 – mateusmaso 2012-07-06 06:18:30