github address: http://github.com/CodeOfficer/js-data-helper/tree/master
What Is It?
It’s a Rails plugin that will help you pass data from Rails to Javascript.
Requirements
JS Data Helper requires either jQuery
1.2.3+, Prototype
1.6.1+ or Mootools
1.2+. Each of those Javascript frameworks has a data/store function built in.
How Does It work?
You use helpers in Rails to convert Ruby values to their Javascript
equivalents. The values are collected and then output to your layout
file just before the closing BODY
tag. The
output is actually inline Javascript, appropriate to the Javascript
framework you are using. It will use the data storage methods of the
Javascript framework you specify to insert data into DOM
elements you have referenced by ID. This will occur inline and before
dom.ready, so you know the data will be available when you need it.
But Why Not Just Use Custom DOM
Attributes?
Sometimes we need to pass data to Javascript. We might decide to use custom attributes to hide data in our DOM
elements but then we would run into a situation where our markup does
not validate. We may also try to hide values in class attributes but
then we’re be stepping on the toes of our designers. I am of the
opinion that data meant for Javascript should remain in Javascript.
Helpers
helper
param
block
notes
js_data_tag |
“dom_id” |
yes |
Used anywhere, the string param represents the dom_id. |
f.js_data |
none
|
yes |
Used inside of a FormBuilder, the dom_id param will be inferred through the Builder and refer to the ID of the Form. |
js_data_functions |
:framework |
none
|
Used in your layout file just before the end BODY
tag. This will output Javascript inline to insert data into your DOM
. Framework is defined as a symbol and must be either :jquery, :prototype or :mootools. |
Example
This demonstration for jQuery shows a variety of test values. I’m using HAML
, but you could just as easily use ERB
. In your view
do something like:
- js_data_tag "test" do |d|
- d.set :test_number, 2
- d.set :test_float, 2.2
- d.set :test_true, true
- d.set :test_false, false
- d.set :test_null_as_string, 'null'
- d.set :test_nil, nil
- d.set :test_NaN, 'NaN'
- d.set :test_UPPERCASE, 'UPPERCASE'
- d.set :test_string, 'some string'
- d.set :test_quoted_function, "function(){ alert('true') }"
- d.set :test_reverse_quoted_function, 'function(){ alert("true") }'
Then throw this in your layout
, you can also specify :mootools or :prototype:
= js_data_functions :jquery
And you will see this in your DOM
:
<script type='text/javascript'>
$('#test').data('railsData', {
test_NaN:NaN,
test_UPPERCASE:'UPPERCASE',
test_false:false,
test_float:2.2,
test_nil:null,
test_null_as_string:null,
test_number:2,
test_quoted_function:function(){ alert('true') },
test_reverse_quoted_function:function(){ alert("true") },
test_string:'some string',
test_true:true
});
</script>
Now in your application.js
(assuming you use Firefox/Firebug and have console.log) you can do:
$(function() {
console.log($('#test').data('railsData'));
});
Though realistically, you might do something more like:
$(function() {
$('.post').each(function(){
var data = $(this).data('railsData');
if (data){
if (data.published){
$(this).find('.title').css('font-weight', 'bold');
} else {
$(this).find('.title').css('color', 'red');
};
};
});
});
分享到:
相关推荐
maven编译pentaho-big-data-plugin遇到的所有问题解决,pentaho6.0
google-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main.zipgoogle-access-helper2022-main....
`build-helper-maven-plugin`是Maven生态系统中的一个重要插件,它扩展了Maven的功能,允许开发者在构建过程中执行一些特殊任务,例如动态添加源代码目录、测试源代码目录,或者处理其他非标准的构建需求。...
java运行依赖jar包
maven-helper-plugin-1.2.7.jar
针对这个问题,我们可以借助"Chrome-Sync-Helper (1).rar"这个压缩包来解决。 "Chrome-Sync-Helper"是一个专为解决Chrome浏览器同步问题设计的小工具,尤其适用于上述情况。它能够帮助用户在重装系统后恢复Chrome的...
"前端开源库-pouchdb-plugin-helper"就是针对PouchDB的一个辅助工具,旨在帮助开发者更轻松地管理和测试PouchDB插件。 PouchDB的核心功能包括: 1. **离线存储**:PouchDB利用HTML5的Web Storage(如IndexedDB或...
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
Laravel内置了强大的本地化和翻译功能,`laravel-js-helper`可以将翻译文件(`resources/lang`)转换成JavaScript对象,前端可以直接使用这些翻译字符串,提升用户体验,减少因为网络延迟导致的翻译加载问题。...
vmware-unity-helper.exe
maven-helper-plugin-1.0.3.jar
drools4-integration-helper-maven-plugin-1.0.1-site
drools4-integration-helper-maven-plugin-1.0.1-sources
drools4-integration-helper-maven-plugin-1.1.0-sources
drools4-integration-helper-maven-plugin-1.1.0-site
drools4-integration-helper-maven-plugin-1.0.1
drools4-integration-helper-maven-plugin-1.1.0
Node.js是一款基于Chrome V8引擎的JavaScript运行环境,它让JavaScript可以在服务器端执行,从而打破了JavaScript只能在浏览器中运行的传统。Node.js具有高性能、轻量级I/O模型以及丰富的模块化生态系统,是开发Web...
helper-maven-插件 helper-maven-插件 回答如何在命令行中获取 Maven groupId、artifactId 和项目版本,
**PyPI 官网下载 | cfn-ci-helper-2021.11.12.2.tar.gz** PyPI(Python Package Index)是Python编程语言的官方软件仓库,为开发者提供了一个平台来发布和分享他们的Python库。"cfn-ci-helper"是一个在PyPI上发布的...