你如何执行AJAX提交并进入backbone.js?

时间:2012-07-23 04:23:08

标签: ruby-on-rails backbone.js

我正在尝试通过AJAX提交提取数据并将其呈现为主干中的div #search_results。我该怎么做呢?我是骨干的新手,还在学习。

谢谢!

RoR控制器:

class ClassroomsController < PublicController
  def search
    @search_result = Classroom.first
    respond_with @search_result
  end
end

RoR查看:

= form_tag search_path, :method => :get, :id => "search_form", do
  = text_field_tag :search_textbox
  = submit_tag "Search", :id => "search_button"
#search_results

教室/ search.js.erb:

$("#search_results").html("<%=escape_javascript(render 'search')%>");

教室/ search.html.haml:

= @search_result.classroom.title

backbone.js查看:

class Classrooms.Views.Classroom extends Backbone.View

  events:
    "submit #search_button": "getResults"

  render: ->
    $(@el).html($("#classroom_main").html())
    @setActiveNavItem("main")
    this

  getResults: (event) ->
    event.stopPropagation()
    event.preventDefault()
    $.get $(this).attr("action"), $(this).serialize(), null, "script"
    return false

1 个答案:

答案 0 :(得分:0)