Rails的使用刚创业板jQuery对象字面多维数组 [英] Rails multi-dimensional array in JQuery Object Literal using Gon Gem

查看:217
本文介绍了Rails的使用刚创业板jQuery对象字面多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的JS&安培; jQuery的。我建立一个调度程序。我在一个名为事件的模式存储约会。我使用一个辅助构建从值多维数组中的所谓event.time_start和event.time_end某一天,一个用户可能预约列。当用户拿起从下拉菜单中START_TIME和END_TIME,已经有约会时间将变灰/已禁用。我使用jquery.timepicker由 http://jonthornton.github.io/jquery-timepicker/随着时间的推移对一个给定的数组中轨,以阻止某些时间值([上午8点,下午6点],[下午1点,下午2])。结果
我发现在某一天使用变量@other_events其他事件(通知其采取倍的)。
搜索结果
这是帮手对的event.start_time和event.end_time结果
events_helper.erb

I am very new to JS & Jquery. I am building a scheduling app. I am storing appointments in a model called "Event". I am using a helper to build the multi-dimensional array from values in columns called "event.time_start" and "event.time_end" for a given day that a user might make an appointment. When a user picks a start_time and end_time from the dropdown menu, times that already have appointments will be grayed out/"disabled". I am using jquery.timepicker by http://jonthornton.github.io/jquery-timepicker/ with rails to blocks certain time values in a given array of time pairs ( [8am, 6pm], [1pm, 2pm]).
I am finding the "other" events on a given day (the ones that inform which times are taken) using the variable @other_events.

this is the helper that pairs the event.start_time and event.end_time
events_helper.erb

module EventsHelper

    def taken_times(other_events)
      other_events.map { |event| [event.time_start, event.time_end] }
      @taken_times = taken_times(other_events)
    end
end

搜索结果
我可以在阵列中的观点是这样工作的:



I can get the array to work like this in the view:

<% taken_times(@other_events).each do |taken_time| %>
  <%= taken_time.first.strftime("%l:%M %P")%>
  <%= taken_time.last.strftime("%l:%M %P")%>,
 <% end %>

但我不能让它在JS工作。我现在想用刚宝石
我需要出去放的是:

But I can't get it to work in the JS. I am now trying to use the Gon Gem What I need to out put to is this:

事件/ show.html.erb视图

events/show.html.erb view

 <script>
     $('#jqueryExample .time').timepicker({
         'timeFormat': 'g:ia',
            'disableTimeRanges':   [
      // start dynamic array
    ['10:00am', '10:15am'],
    ['1:00pm', '2:15pm'],
    ['5:00pm', '5:15pm']
]
      //end dynamic array
     });
        $('#jqueryExample').datepair();
        </script>

所以我试过

application.html.erb

application.html.erb

  <%= Gon::Base.render_data({}) %>

events_controller.erb

events_controller.erb

def show
 @event = Event.new
 @other_events = Event.where(date_id: params[:id])
 gon.taken_times = @taken_times
end

与JS

 <script>
     $('#jqueryExample .time').timepicker({
         'timeFormat': 'g:ia',
            'disableTimeRanges':   [
      // start dynamic array
     gon.taken_times
      //end dynamic array
     });
        $('#jqueryExample').datepair();
        </script>

但是,这并不正常工作。它只是杀死文本框的下拉作用。我很想知道这些逗号分隔的对象,如'TIMEFORMAT':'G:IA,和disableTimeRanges:[......]被称为这样我就可以更好地为JavaScript的答案,所以搜索。我是很新的JS&安培; JQuery的。

but this doesn't work. it just kills the dropdown function of the textfields. I'd love to know what these comma separated objects like "'timeFormat': 'g:ia'," and "disableTimeRanges: [....]" are called so I can better search SO for javascript answers. I am very new to JS & JQuery.

推荐答案

刚似乎处理这个问题的正确途径,但也有在code的各种错误:

Gon seems the right way to handle this but there are various errors in your code:

你在哪里写的&LT;%=刚:: Base.render_data({})%GT; ?它应该是在&LT; HEAD&GT; 标记之前,任何JavaScript code

Where do you write <%= Gon::Base.render_data({}) %> ? It should be in the <head> tag, before any javascript code

event_helper.rb 是错误的:

def taken_times(other_events)
  # Map returns a **copy**, if you don't assign it to anything, it's just lost
  other_events.map { |event| [event.time_start, event.time_end] }
  # This has **completely no sense**, this method will throw a stack level too deep error **every time you call it**, you should read a book about programming before trying to use rails
  @taken_times = taken_times(other_events)
end

如果您想使用,去像

def taken_times(other_events)
  other_events.map { |event| [event.time_start, event.time_end] }
end

该控制器具有同样的问题previous code

The controller has the same problem of previous code

def show
 @event = Event.new
 @other_events = Event.where(date_id: params[:id])
 # @taken_times is not assigned, gon.taken_times will always be `null` in JS
 gon.taken_times = @taken_times
end

您应该重写它像:

helper :taken_times
def show
 @event = Event.new
 @other_events = Event.where(date_id: params[:id])
 gon.taken_times = taken_times(@other_events)
end

最后,你必须在你的JavaScript code大的语法错误,这就是为什么一切都消失了。另外你正在使用坤,如果是一些特别的东西。刚只是创建一个普通的 JavaScript对象上,您可以访问它在JavaScript就像你与别的事。

And finally, you have a big syntax error in your javascript code, that's why everything disappear. Also you are using gon if it's something special. Gon just creates a plain javascript object, you can access it in javascript like you would do with anything else.

 <script>
     $('#jqueryExample .time').timepicker({
         'timeFormat': 'g:ia',
            // Here you have an opening square brackets, which is never closed
            // Also, there is no point in having a square brackets here or you'll have an array of array of array, while you want an array of array
            'disableTimeRanges':   [
      // start dynamic array
     gon.taken_times
      //end dynamic array
     });
        $('#jqueryExample').datepair();
        </script>

这不是应

 <script>
     $('#jqueryExample .time').timepicker({
         'timeFormat': 'g:ia',
            'disableTimeRanges': gon.taken_times
     });
        $('#jqueryExample').datepair();
        </script>

我的主要建议是,读一本书的Ruby第一个,而不是一个使用Ruby和Rails的合并,你显然需要做任何事情之前,以改善您的编程技巧。开始像的良好接地Rubyist 并跟进一本好书关于JavaScript(这我还没有找到,关于它的评论是好AP preciated)。然后去轨道4在行动并在这一点上,你可能更好地了解发生了什么事情。

My main suggestion is, read a Ruby book first, not one with Ruby and Rails merged, you clearly need to improve your programming skills before doing anything. Start with something like The Well Grounded Rubyist and follow up with a good book about javascript (which I have yet to find, comments about it are well appreciated). Then go for Rails 4 in Action and at this point you probably understand better what's going on.

<一个href=\"http://www.quora.com/Whats-the-best-way-for-a-beginner-to-start-learning-Ruby-on-Rails/answer/Francesco-Belladonna\"相对=nofollow>这是我的食谱要成为一个好Rails开发

这篇关于Rails的使用刚创业板jQuery对象字面多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆