- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在我的页面中实现实时搜索功能。我有以下简化的 sinatra Controller
get '/search_item/:for' do
//do some search stuff
haml :search_item,:layout => (request.xhr? ? false : :layout),
:locals => { :found_items => queried_items,
:current_user => current_user,
}
end
然后我有一个带有输入字段的layout.haml文件
%input(type="text" id="search_query" name="search_query" size="15" value="")
和两个div
#content
= yield
#results(style="display=none")
现在开始我的问题我需要一个 jquery/javascript 函数来发送 xhr 或 json 请求,使用 sinatra Controller 中的输入生成 search_item View ,然后应在布局 #results div 中渲染/加载该 View ,而无需刷新整个页面。现在我已经有了类似的东西:
:javascript
$(function() {
$("#search_query").keyup(function(){
var q = $("#search_query").val();
if(q != ""){
----------- this is the problem part -------------
$.get('/search_item/'+q, function(data) {
$('#results').html(data);})
--------------------------------------------------
$("#content").css("display","none");
$("#results").css("display","inline");
}
else{
$("#content").css("display","inline");
$("#results").css("display","none");
}}); });
有没有一种方法可以像这样简单地将路由的 haml View 加载到 div 中
var url = "/search_item/"+q;
$('#results').load(url);
我尝试过,但它不起作用..
我没有使用rails!所以我有三个问题:
预先非常感谢您的帮助!
最佳答案
How do I properly send the request on keyup to sinatra and how can i use it there?
您需要在$.get()
中将数据类型指定为html
How do I then without refreshing render the search_item.haml into the layout.haml #results div
首先,您需要将 haml View 编译为 html(浏览器不支持 haml,但 haml 编译为您已经在做的 html/erb 模板!),然后您最好的方法可能是使用 $('some_selector').append( data_recieved_from_get_call)
或 $('some_selector').html( data_recieved_from_get_call)
。
Is there a more elegant way to do it?
我建议查看backbone.js ,它是一种用于管理单个页面 UI 的 javascript 框架。
一旦你掌握了它,我建议你查看 backbone.marionette.js 。您最终会仅用主干来重新发明轮子,因此一旦您了解了主干的工作原理,这将真正能够提高工作效率。
最后但并非最不重要的一点是,coffeescript 。它更具可读性!
这三件事需要一些时间来理解,但这是值得的!!
关于jquery - 通过 Sinatra Controller 根据 Ajax 请求将 haml View 加载到布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13541296/
您在 Sinatra 应用程序中使用什么验证码? 我喜欢 Google 的 http://www.google.com/recaptcha ,但似乎不适用于 Sinatra(不过有一个 Rails 插
如何从命令行将 Sinatra 应用作为守护程序运行? 它正在使用瘦: ruby app.rb -p 3000 -e production 我不喜欢在 app.rb 本身中设置它。我想从命令行取消它。
我正在使用 Sinatra 在 Ruby 中编写一个小型 Web 服务。使用 http 基本身份验证(在生产中通过 https)控制对几乎所有内容的访问。 我想从要求授权中排除一个特定的目录。是否有捷
我曾经能够在启动 sinatra 应用程序的窗口中看到为 sinatra 发出的 http 请求。 我认为在 sinatra 升级后我再也看不到它们了,我不知道如何做到这一点。我不需要将它们记录到文件
带有 activerecord gem 的 Sinatra 项目发出警告: DEPRECATION WARNING: Currently, Active Record suppresses error
我正在玩 Sinatra,我想让我的一条路线不区分大小写。我尝试添加这样的路线: get "(?i)/tileflood/?" do end 但它与预期的/tileflood 的任何排列都不匹配。我在
如果我没有在 Sinatra 中明确设置 session 过期时间,那么默认的过期时间是多少? 最佳答案 Sinatra session 来自 Rack::Session;所以开箱即用它默认为不过期。
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
假设我有一条 Sinatra 路线: put '/data' do request.body.read # ... end 看起来整个 request.body 都被读入内存了。有没有办法在主
我正在 Sinatra 中运行一个经典风格的应用程序,我想获取该应用程序绑定(bind)到的 URL。例如,如果我在开发环境中启动它,我希望得到:http://localhost:4567/,而在生产
我正在尝试在我的 Mac 上安装以下 ruby gem: http://www.sinatrarb.com/intro.html 我收到以下消息: michael-rosarios-mac
我最近更新到了 Ruby 1.9.2 (RVM)、Sinatra 1.1 和 Passenger 3.0.0。我有一个简单的应用程序,由以下部分组成: # config.ru require 'rub
似乎没有关于如何使用 Sinatra 中的 Rack::Protection 为表单启用 CSRF 保护的文档。有人做过这个吗?我似乎不知道如何告诉 Sinatra 启用此功能。 最佳答案 Sinat
与 sinatra 应用程序相比,Rack 应用程序有哪些优势? 谢谢 最佳答案 我认为提问者想要将它保留在 Rack 上的优点。 考虑到 Sinatra 已经是典型 Web 应用程序的极薄和简约的贴
我最近创建了一个带有登录表单(无基本身份验证)的 Sinatra 应用程序。为了防止在用户登录之前访问该应用程序,我在适当的位置放置了一个 before block before do unles
我如何设置 Sinatra 以便公用文件夹中的静态文件与响应 Access-Control-Allow-Origin = "*"一起返回? 最佳答案 在这里查看这个问题:Sinatra OPTIONS
我有一个基于 Sinatra 的 REST 服务应用程序,我想从其中一个路由中调用其中一个资源,从而有效地将一个资源与另一个资源组合在一起。例如 get '/someresource' do ot
我有一个使用经典 Sinatra 的旧项目(不是模块化 我在互联网上能找到的唯一使用 Puma 的示例是使用模块化 Sinatra。 引用:http://www.sinatrarb.com/intro
问题是默认处理程序是在 中定义的。 Sinatra::ShowExceptions ,由 控制:raise_errors 配置设置,这个设置是全有或全无。如果 :raise_errors 关闭然后 错
我有以下 Sinatra 应用程序,我正在测试 error阻止但它似乎不起作用。 这是我的 sinatra 应用程序: require 'rubygems' require 'sinatra' err
我是一名优秀的程序员,十分优秀!