我目前有一个Rails网站,里面有一些Prototype脚本。然而,我的老板要我转换页面,以便它使用JQUERY库。我没有太多的Javascript编程经验所以我有点困惑。我基本上下载了一个名为'jrails'的插件,它有助于移动Prototype的'script.aculo.us'所需的助手,并将它们翻译成JQUERY可以理解的东西
以下是两个代码:
原型:
new Form.Element.Observer('new_member_search', 0.5, function(element, value){
new Ajax.Updater('ProjectMemberNew', '/of/user/search', {
asynchronous:true,
evalScripts:true,
parameters:'username=' + encodeURIComponent(value)
});
});
Jquery的:
$('#new_member_search').delayedObserver(0.5, function(element, value){
$.ajax({
data:'username=' + value,
success:function(request){
$('#ProjectMemberNew').html(request);
},
type:'post',
url:'/of/user/search'
});
});
原型代码工作正常,但Jquery似乎没有出于某种原因运行。顺便说一句,我没有改变View中的任何内容,只是在页面中包含了“jrails”。知道为什么这不起作用吗? 谢谢,
编辑1:
所以我在Firebug中运行了一些测试,我想也许我应该发布原始的rails代码:
<%= drop_receiving_element drop_id,
:onDrop => "function(drag_element, drop_element, event){
if (confirm(\"#{escape_javascript(_('This will remove User from this Group, are you sure?'))}\"))
{#{remote_function(:update => 'module_content',
:url => {:controller => :projects,
:action => :member_delete,
:id => @project.id},
:with => "'u=' + encodeURIComponent(drag_element.id)"
)};}
}",
:accept => 'RolesUsersSelection',
:hoverclass => "#{drop_class}_active"
%GT;
当我在firebug上运行网页时,jquery版本给了我一些错误:
Prototype is not defined
[Break On This Error] linear: Prototype.K,
effect...2478493 (line 71)
$.effects is undefined
[Break On This Error] $.effects.drop = function(o) {
jquery...4111587 (line 15)
$("#RemoveThisMember").droppable is not a function
[Break On This Error] }, hoverClass:'ProjectRoleDropDelete_active'})
member_edit (line 323)
$("#role_15_user_4").draggable is not a function
[Break On This Error] $("#role_15_user_4").draggable({revert:true})
有些东西告诉我,我无法转移到jquery库而不对rails代码进行任何更改..
答案 0 :(得分:0)
你必须首先初始化jquery,然后是jquery-ui然后jrails。如果它不是那个orderObserver或任何其他助手不会初始化。确实很奇怪。 https://github.com/aaronchi/jrails/issues#issue/6