UNPKG

1.51 kBtext/coffeescriptView Raw
1#
2# batman.jquery.coffee
3# batman.js
4#
5# Created by Nick Small
6# Copyright 2011, Shopify
7#
8
9# Include this file instead of batman.nodep if your
10# project already uses jQuery. It will map a few
11# batman.js methods to existing jQuery methods.
12
13
14Batman.Request::send = (data) ->
15 options =
16 url: @get 'url'
17 type: @get 'method'
18 dataType: @get 'type'
19 data: data || @get 'data'
20 username: @get 'username'
21 password: @get 'password'
22 beforeSend: =>
23 @fire 'loading'
24
25 success: (response, textStatus, xhr) =>
26 @set 'status', xhr.status
27 @set 'response', response
28 @fire 'success', response
29
30 error: (xhr, status, error) =>
31 @set 'status', xhr.status
32 @set 'response', xhr.responseText
33 xhr.request = @
34 @fire 'error', xhr
35
36 complete: =>
37 @fire 'loaded'
38
39 if @get('method') in ['PUT', 'POST']
40
41 unless @get 'formData'
42 options.contentType = @get 'contentType'
43 else
44 options.contentType = false
45 options.processData = false
46 options.data = @constructor.objectToFormData(options.data)
47
48 jQuery.ajax options
49
50Batman.mixins.animation =
51 show: (addToParent) ->
52 jq = $(@)
53 show = ->
54 jq.show 600
55
56 if addToParent
57 addToParent.append?.appendChild @
58 addToParent.before?.parentNode.insertBefore @, addToParent.before
59
60 jq.hide()
61 setTimeout show, 0
62 else
63 show()
64 @
65 hide: (removeFromParent) ->
66 $(@).hide 600, =>
67 @parentNode?.removeChild @ if removeFromParent
68 @