- 浏览: 2073287 次
- 性别:
- 来自: NYC
文章分类
- 全部博客 (628)
- Linux (53)
- RubyOnRails (294)
- HTML (8)
- 手册指南 (5)
- Mysql (14)
- PHP (3)
- Rails 汇总 (13)
- 读书 (22)
- plugin 插件介绍与应用 (12)
- Flex (2)
- Ruby技巧 (7)
- Gem包介绍 (1)
- javascript Jquery ext prototype (21)
- IT生活 (6)
- 小工具 (4)
- PHP 部署 drupal (1)
- javascript Jquery sort plugin 插件 (2)
- iphone siri ios (1)
- Ruby On Rails (106)
- 编程概念 (1)
- Unit Test (4)
- Ruby 1.9 (24)
- rake (1)
- Postgresql (6)
- ruby (5)
- respond_to? (1)
- method_missing (1)
- git (8)
- Rspec (1)
- ios (1)
- jquery (1)
- Sinatra (1)
最新评论
-
dadadada2x:
user模型里加上 protected def email ...
流行的权限管理 gem devise的定制 -
Sev7en_jun:
shrekting 写道var pattern = /^(0| ...
强悍的ip格式 正则表达式验证 -
jiasanshou:
好文章!!!
RPM包rpmbuild SPEC文件深度说明 -
寻得乐中乐:
link_to其实就是个a标签,使用css控制,添加一个参数: ...
Rails在link_to中加参数 -
aiafei0001:
完全看不懂,不知所然.能表达清楚一点?
"$ is not defined" 的问题怎么办
这是一个特别的,不太正统的需求,
因为,大部分时候,ajax的返回,只要返回json类型数据就可以了
然而,如果返回的是partial,partial里有变量是hash,而这个hash又需要jquery的plugin例如图表之类的用,那就需要这个rails的hash转成javascript能认,能读的hash了。
思路呢,还是通过json
就是在haml或者erb页面里吧hash转json
然后
或者
eval
另外,这样的转换,需要比较标准的json格式,要一般的转换参考用下面的
https://github.com/douglascrockford/JSON-js
因为,大部分时候,ajax的返回,只要返回json类型数据就可以了
然而,如果返回的是partial,partial里有变量是hash,而这个hash又需要jquery的plugin例如图表之类的用,那就需要这个rails的hash转成javascript能认,能读的hash了。
思路呢,还是通过json
就是在haml或者erb页面里吧hash转json
然后
jQuery.parseJSON( json )
var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name === "John" );
或者
eval
另外,这样的转换,需要比较标准的json格式,要一般的转换参考用下面的
https://github.com/douglascrockford/JSON-js
引用
JavaScript is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. It is still widely believed to be a subset of Java, but it is not. It is a Scheme-like language with C-like syntax and soft objects. JavaScript was standardized in the ECMAScript Language Specification, Third Edition.
JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.
Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.
myData = JSON.parse(text, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value);
}
}
return value;
});
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
Values that do not have a representation in JSON (such as functions and undefined) are excluded.
Nonfinite numbers are replaced with null. To substitute other values, you could use a replacer function like this:
function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
}
JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.
Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.
myData = JSON.parse(text, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value);
}
}
return value;
});
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
Values that do not have a representation in JSON (such as functions and undefined) are excluded.
Nonfinite numbers are replaced with null. To substitute other values, you could use a replacer function like this:
function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
}
var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] };
发表评论
-
Destroying a Postgres DB on Heroku
2013-04-24 10:58 931heroku pg:reset DATABASE -
VIM ctags setup ack
2012-04-17 22:13 3256reference ctags --extra=+f --e ... -
alias_method_chain方法在3.1以后的替代使用方式
2012-02-04 02:14 3289alias_method_chain() 是rails里的一个 ... -
一些快速解决的问题
2012-01-19 12:35 1470问题如下: 引用Could not open library ... -
API service 安全问题
2011-12-04 08:47 1380这是一个长期关注的课题 rest api Service的 ... -
Module方法调用好不好
2011-11-20 01:58 1344以前说,用module给class加singleton方法,和 ... -
一个ajax和rails交互的例子
2011-11-19 01:53 1903首先,这里用了一个,query信息解析的包,如下 https: ... -
关于Rubymine
2011-11-18 23:21 2265开个帖子收集有关使用上的问题 前一段时间,看到半价就买了。想 ... -
ruby中和javascript中,动态方法的创建
2011-11-18 21:01 1235class Klass def hello(*args) ... -
textmate快捷键 汇总
2011-11-16 07:20 8139TextMate 列编辑模式 按住 Alt 键,用鼠标选择要 ... -
Ruby面试系列六,面试继续面试
2011-11-15 05:55 2018刚才受到打击了,充分报漏了自己基础不扎实,不肯向虎炮等兄弟学习 ... -
说说sharding
2011-11-13 00:53 1485这个东西一面试就有人 ... -
rails面试碎碎念
2011-11-12 23:51 1942面试继续面试 又有问ru ... -
最通常的git push reject 和non-fast forward是因为
2011-11-12 23:29 17210git push To git@github.com:use ... -
Rails 自身的many to many关系 self has_many
2011-11-12 01:43 2733简单点的 #注意外键在person上people: id ... -
Rails 3下的 in place editor edit in place
2011-11-12 01:20 945第一个版本 http://code.google.com/p ... -
Heroku 的诡异问题集合
2011-11-11 07:22 1693开个Post记录,在用heroku过程中的一些诡异问题和要注意 ... -
SCSS 和 SASS 和 HAML 和CoffeeScript
2011-11-07 07:52 12952Asset Pipeline 提供了内建 ... -
Invalid gemspec because of the date format in specification
2011-11-07 02:14 2116又是这个date format的错误。 上次出错忘了,记录下 ... -
ruby面试系列五,面试题及其他
2011-11-05 21:18 7402周六早晨,因为早了所 ...
相关推荐
介绍了如何扩展Hash类,使其更具功能性。 **16.4 字符串扩展** 探讨了如何通过ActiveSupport扩展String类。 **16.5 数值的扩展** 讲解了如何通过ActiveSupport扩展数字类。 **16.6 时间和日期的扩展** 介绍了...
带有预配置 Javascript、AJAX 数据加载、干净的 HTML 和帮助程序的 Rails 应用程序的漂亮图表 安装 将此行添加到应用程序的 Gemfile 中: gem 'rails_chart' 然后执行: $ bundle 或者自己安装: $ gem ...
9.5 迭代D5:JavaScript被禁用时的对策 99 9.6 我们做了什么 99 第10章 任务E:付账! 101 10.1 迭代E1:收集订单信息 101 第11章 任务F:管理 113 11.1 迭代F1:添加用户 113 11.2 迭代F2:登录 120 11.3 迭代F3:...
运行 `rails generate scaffold post title filename qiniu_hash` 命令,将生成处理图片发表所需的模型、控制器、视图和数据库迁移文件。执行 `rake db:migrate` 完成数据库迁移,然后在浏览器中访问 `...
请参阅以了解详细信息安装将此行添加到您的应用程序的Gemfile中: gem 'jquery_bbq_rails' 将以下指令添加到您的Javascript清单文件(application.js): //= require jquery_bbq版本控制jquery_b
用于 Rails 的 SnackbarJS 见演示: : 安装将此行添加到应用程序的 Gemfile 中: gem 'snackbarjs-rails' 然后执行: $ bundle或者自己安装: $ gem install snackbarjs-rails用法应用程序.js //= require snackbar ...
在Ruby on Rails(简称Rails)框架中,字典通常指的是哈希(Hash),它是Ruby语言的一个核心数据结构,用于存储键值对。在Rails中,哈希常用于表示数据库记录,如ActiveRecord对象的属性,或者在视图中传递上下文...
3. **设置数据库**:更新迁移文件以添加必要的字段,如username、email和password_hash等,然后运行`rails db:migrate`。 4. **配置Authlogic**:在User模型中启用Authlogic,设置加密策略(如BCrypt)并配置其他...
- **迭代D5:JavaScript被禁用时的对策**:确保在JavaScript未启用的情况下也能正常使用购物车功能。 #### 第10章:任务E:付账 - **迭代E1:收集订单信息**:获取用户的订单信息,为结账做准备。 #### 第11章:...
leetcode和洛谷 令人敬畏的毒液 :deer: Una colección de recursos curados por y para la casa de los venados。...Rails ...Rails ...Javascript ...JavaScript ...Aircrack-ng/Hashcat 破解 WPA/WPA2 Wi-Fi 路由器
5. **版本控制**:Sprockets通过添加指纹(hash值)到打包文件名来实现缓存 busting,确保当资源更新时,浏览器会获取新的版本而不是使用缓存的旧版本。这在部署新版本应用时非常有用,避免了用户看到过期的样式或...
接下来是Ruby,一种注重简洁性和生产力的动态类型语言,常用于Web开发(如Rails框架)。Ruby挑战可能涵盖: 1. 元编程:Ruby的动态特性允许在运行时修改类和对象,这是其元编程能力的体现。 2. 魔法方法:了解如`...
在这个特定项目中,团队通过Rails搭建了一个后端系统,该系统能够接收歌词数据,进行处理,并返回词频统计结果。 在Ruby中,可以使用内建的数据结构如哈希(Hash)来存储每个单词及其出现的次数,然后通过遍历歌词...
该软件包包括一个视图帮助器,javascript事件侦听器和一个服务器端通道帮助器方法,该方法在设置时将在用户更改字段值时自动更新您的应用程序数据库和视图。 基本范例: < %= phx_in_place @ product , : ...
- **Hash/Routing & Bookmarking**:使用浏览器的历史API和hash值来进行页面导航。 - **routes.js**:轻量级的路由管理库。 - **History.js**:支持多种浏览器的历史管理。 - **Ben Alman 的BBQ & hashchange**:...
- **游戏开发:** 虽然不如Python或JavaScript那样常用,Ruby也可以用于游戏开发。 通过上述知识点的总结,我们可以看出Ruby不仅是一种功能强大的编程语言,而且具有广泛的适用性和灵活性,适用于多种不同的开发...
2. **用户界面**:使用Ruby on Rails框架构建的Web应用程序通常包含HTML、CSS和JavaScript,用于创建用户友好的界面。Rails的erb模板引擎将Ruby代码嵌入到HTML中,使得动态内容生成成为可能。 3. **数据库集成**:...
- **Ruby on Rails**:Ruby on Rails是一个流行的服务器端Web应用框架,用于构建数据库驱动的应用程序。 #### JSON简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,也便于...
JavaScript 函数是第一类公民,这意味着它们可以赋值给变量、作为参数传递以及返回其他函数。此外,JavaScript 支持闭包,使得函数可以访问它们创建时的作用域内的变量。 ### HTML HTML (HyperText Markup ...