【问题标题】:Rails, Gritter - remove default titleRails,Gritter - 删除默认标题
【发布时间】:2017-10-14 12:47:59
【问题描述】:

我在 Rails 5 应用程序中使用 Gritter 通知,但找不到删除通知弹出窗口默认标题的方法。我将 Gritter 添加如下:

<%= js add_gritter(flash[:notice], title: 'I dont need you', sticky: false) %>

试过了:

<%= js add_gritter(flash[:notice], title: false, sticky: false) %>
<%= js add_gritter(flash[:notice], title: nil, sticky: false) %>
<%= js add_gritter(flash[:notice], title: '', sticky: false) %>
<%= js add_gritter(flash[:notice], title: ' ', sticky: false) %>

并且弹出窗口仍然显示默认标题 - “通知”。试图在整个应用程序的项目“通知”或“Gritter”中搜索,但没有找到相关的。

如何摆脱它?

【问题讨论】:

    标签: ruby-on-rails gritter


    【解决方案1】:

    如果 options[:title].blank? 返回 true,则 gem 中的 add_gritter 方法将 options[:title] 设置为“通知”。

    “脏”选项是再次定义它,用选项哈希代替*args,如果标题作为选项参数传递,则呈现标题,例如:

    def add_gritter(text, options={})
      if %w(success warning error notice progress).include?(options[:image].to_s)
        options[:image] = image_path("#{options[:image]}#{options[:image].to_s == 'progress' ? '.gif' : '.png'}") 
      end
      notification = Array.new
      notification.push("jQuery(function(){") if options[:nodom_wrap].blank?
      notification.push("jQuery.gritter.add({")
      notification.push("image:'#{options[:image]}',") if options[:image].present?
      notification.push("sticky:#{options[:sticky]},") if options[:sticky].present?
      notification.push("time:#{options[:time]},") if options[:time].present?
      notification.push("class_name:'#{options[:class_name]}',") if options[:class_name].present?
      notification.push("before_open:function(e){#{options[:before_open]}},") if options[:before_open].present?
      notification.push("after_open:function(e){#{options[:after_open]}},") if options[:after_open].present?
      notification.push("before_close:function(e){#{options[:before_close]}},") if options[:before_close].present?
      notification.push("after_close:function(e){#{options[:after_close]}},") if options[:after_close].present?
      notification.push("on_click:function(e){#{options[:on_click]}},") if options[:on_click].present?
      notification.push("title:'#{escape_javascript(options[:title])}',") if options[:title].blank? # Here
      notification.push("text:'#{escape_javascript(text)}'")
      notification.push("});")
      notification.push("});") if options[:nodom_wrap].blank?
      text.present? ? notification.join.html_safe : nil
    end
    

    但是 gritt.js 文件有一个if 来检查title 是否有任何内容,所以你应该自己处理并编辑它,只是为了检查文本,比如:

    //We might have some issues if we don't have a title or text!
    if (!params.text) {
      throw 'You need to fill out the text parameter.';
    }
    

    【讨论】:

      【解决方案2】:

      听起来不是最好的方法,但很有效。如果有任何其他解决方案 - 请随意:)

      .gritter-title {
        display: none;
      }
      

      编辑:

      因为我用的是SCSS,这个比较好:

      <%= js add_gritter(flash[:notice], sticky: false, :on_click => remove_gritter, :time => 100000, class_name: 'no-title') %>
      .no-title {
        .gritter-title {    
            display: none;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-10
        相关资源
        最近更新 更多