Last active
December 10, 2019 07:28
-
-
Save dapi/a05239b8ab10e7871de8547475459724 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Author: Danil Pismenny https://github.com/dapi | |
| # | |
| # Install: | |
| # | |
| # add gem 'rails-assets-noty' with source 'https://rails-assets.org' | |
| # | |
| # Usage: | |
| # | |
| # window.Flash.show('error', 'message') | |
| # | |
| # or | |
| # | |
| # window.Flash.error('message') | |
| # | |
| #= require noty | |
| # | |
| TYPES = ['error', 'success', 'info', 'warning'] | |
| TIMEOUT = 5000 | |
| window.Flash = | |
| show: (type, message) -> new Noty(text: message, timeout: TIMEOUT, type: type).show() | |
| for type in TYPES | |
| window.Flash[type] = (message) -> @show(type, message) | |
| # Хендлеры имеет смысл отдавать в AJAX-запросы следующим образом | |
| # | |
| # xhr = $.ajax | |
| # fail: window.ajaxFailHanlder | |
| # error: window.ajaxErrorHandler response, message | |
| # | |
| window.ajaxErrorHandler = (response) -> | |
| # readyState Holds the status of the XMLHttpRequest. | |
| # 0: request not initialized | |
| # 1: server connection established | |
| # 2: request received | |
| # 3: processing request | |
| # 4: request finished and response is ready | |
| return if response.readyState == 0 | |
| message = response.responseText || response.statusText | |
| console.error? "Ошибка загрузки #{message}" | |
| Flash.error message | |
| window.ajaxFailHanlder = (response) -> | |
| return if response.readyState == 0 | |
| console.error? "Fail загрузки #{response}" | |
| Flash.error response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #= require noty | |
| # | |
| # Usage: | |
| # | |
| # add gem 'rails-assets-noty' with source 'https://rails-assets.org' | |
| # | |
| TIMEOUT = 5000 | |
| window.Flash = | |
| error: (message) -> new Noty(text: message, timeout: TIMEOUT, type: 'error').show() | |
| success: (message) -> new Noty(text: message, timeout: TIMEOUT, type: 'success').show() | |
| info: (message) -> new Noty(text: message, timeout: TIMEOUT, type: 'information').show() | |
| warning: (message) -> new Noty(text: message, timeout: TIMEOUT, type: 'warning').show() | |
| window.ajaxErrorHandler = (response) -> | |
| # readyState Holds the status of the XMLHttpRequest. | |
| #0: request not initialized | |
| #1: server connection established | |
| #2: request received | |
| #3: processing request | |
| #4: request finished and response is ready | |
| return if response.readyState == 0 | |
| message = response.responseText || response.statusText | |
| console.error? "Ошибка загрузки #{message}" | |
| Flash.error message | |
| window.ajaxFailHanlder = (response) -> | |
| return if response.readyState == 0 | |
| console.error? "Fail загрузки #{response}" | |
| Flash.error response |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Хелпер показывает рельсовые флешки через window.Flash | |
| # | |
| module FlashesHelper | |
| DEFAULT_TYPE = :warning | |
| TYPES = { alert: :error, notice: :info } | |
| def noty_flash_javascript(key, message) | |
| noty_type = TYPES[key.to_sym] || DEFAULT_TYPE | |
| "window.Flash.show(#{noty_type.to_json}, #{message.to_json})" | |
| end | |
| end |
dapi
commented
Nov 23, 2018
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment