Created
September 19, 2015 09:39
-
-
Save fpaint/95e4ab72dcbe8904de49 to your computer and use it in GitHub Desktop.
Ajax module in my project
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.OrhApp ||= { } | |
| OrhApp.Ajax = (settings) -> | |
| ajax = this | |
| @error_proc = (proc)-> | |
| (ctx)-> | |
| proc?(ctx).fail (xhr)-> | |
| if xhr.status == 404 and match = xhr.responseText.match /^Redirect: (.*)$/ | |
| location.href = match[1] | |
| return | |
| message = xhr.statusText || 'Ошибка запроса' | |
| message = 'Ошибка запроса' if xhr.status == 500 | |
| message = 'Ошибка: не найден объект.' if xhr.status == 404 | |
| message = xhr.responseText if xhr.status == 400 | |
| Orh.notice.show(message) | |
| # Кнопка, запускающая асинхронную задачу и залипающую до её завершения | |
| @button = (el, proc)-> | |
| btn = $(el) | |
| states = | |
| idle: | |
| on_click: (btn, state)-> | |
| state.turn('pending') | |
| pending: | |
| set: (btn, state)-> | |
| btn.addClass('pending').prop('disabled', true) | |
| ajax.error_proc(proc)?(btn).done( -> state.trigger('done')).fail( (xhr)-> state.trigger('fail', xhr)) | |
| unset: (btn, state)-> | |
| btn.removeClass('pending').prop('disabled', false) | |
| on_done: (btn, state)-> | |
| state.turn('idle') | |
| on_fail: (btn, state, xhr)-> | |
| state.turn('idle') | |
| state = new OrhApp.StateMachine states, btn | |
| state.bind_events(btn) | |
| btn.click -> state.trigger 'click' | |
| state | |
| # Кнопка, становящаяся активной после завершения задачи | |
| @activeButton = (el, proc)-> | |
| state = @button(el, proc) | |
| state.extend | |
| pending: | |
| on_done: (btn, state)-> | |
| state.turn('active') | |
| active: | |
| set: (btn, state)-> | |
| btn.addClass('active') | |
| unset: (btn, state)-> | |
| btn.removeClass('active') | |
| state | |
| @tabButton = (key, el, block)-> | |
| btn = $(el) | |
| states = | |
| idle: | |
| on_click: (ctx, state)-> | |
| state.turn('active') | |
| active: | |
| set: (ctx, state)-> | |
| btn.addClass('active') | |
| block.addClass('active') | |
| block.trigger('orh:activate') | |
| unset: (ctx)-> | |
| btn.removeClass('active') | |
| block.removeClass('active') | |
| block.trigger('orh:deactivate') | |
| on_close: (ctx, state)-> | |
| state.turn('idle') | |
| state = new OrhApp.StateMachine states, {} | |
| state.bind_events(btn) | |
| btn.click -> state.trigger 'click' | |
| state | |
| tabButtons = -> | |
| buttons = {} | |
| @is_empty = -> $.isEmptyObject(buttons) | |
| @append = (key, state)-> buttons[key] = state | |
| @activate = (key)-> | |
| _.each buttons, (state, i)-> | |
| state.turn if key == i then 'active' else 'idle' | |
| return | |
| @htmlTabs = (control, default_key)-> | |
| buttons = new tabButtons | |
| @append = (key, btn, block)-> | |
| button = $(btn).attr('data_key', key).removeClass('hidden') | |
| button.appendTo(control) if button.parent().length == 0 | |
| state = Orh.ajax.tabButton key, button, block | |
| state.extend | |
| idle: | |
| on_click: -> buttons.activate(key) | |
| activate = (!default_key and buttons.is_empty()) or key == default_key | |
| buttons.append(key, state) | |
| buttons.activate(key) if activate | |
| $(block).on 'click', '.close', -> state.turn('idle') | |
| return button | |
| return | |
| # Табы из активных кнопок, загружающие контент в контейнер | |
| @tabs = (control, container, default_key, settings={}) -> | |
| buttons = new tabButtons | |
| @append = (key, caption, url)-> | |
| button = $('<button>').addClass('btn').attr('data_key', key).html(caption).appendTo(control) if typeof caption is 'string' | |
| button = caption if typeof caption is 'object' | |
| proc = -> | |
| $.get url, (res)-> | |
| $(container).html(res) unless settings.replace | |
| $(container).replaceWith(res) if settings.replace | |
| state = Orh.ajax.activeButton button, proc | |
| state.extend | |
| pending: | |
| on_done: -> buttons.activate(key) | |
| activate = (!default_key and buttons.is_empty()) or key == default_key | |
| buttons.append(key, state) | |
| buttons.activate(key) if activate | |
| return button | |
| return | |
| # Подрузка контента по кнопочке | |
| @content_loader = (control, container, url)-> | |
| url || = location.pathname | |
| page = 1 | |
| @button control, -> | |
| return $.get url, {page: page+1}, (res, status, xhr)-> | |
| $(control).hide() if xhr.getResponseHeader('Last-Page') == 'true' | |
| if status == 'success' | |
| $(container).append(res) | |
| page += 1 | |
| else if(status == 'nocontent') | |
| $(control).hide() | |
| return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment