batman-rails 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,71 +0,0 @@
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
-
14
- Batman.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
- headers: @get 'headers'
23
- beforeSend: =>
24
- @fire 'loading'
25
-
26
- success: (response, textStatus, xhr) =>
27
- @set 'status', xhr.status
28
- @set 'response', response
29
- @fire 'success', response
30
-
31
- error: (xhr, status, error) =>
32
- @set 'status', xhr.status
33
- @set 'response', xhr.responseText
34
- xhr.request = @
35
- @fire 'error', xhr
36
-
37
- complete: =>
38
- @fire 'loaded'
39
-
40
- if @get('method') in ['PUT', 'POST']
41
-
42
- unless @hasFileUploads()
43
- options.contentType = @get 'contentType'
44
- else
45
- options.contentType = false
46
- options.processData = false
47
- options.data = @constructor.objectToFormData(options.data)
48
-
49
- jQuery.ajax options
50
-
51
- Batman.mixins.animation =
52
- show: (addToParent) ->
53
- jq = $(@)
54
- show = ->
55
- jq.show 600
56
-
57
- if addToParent
58
- addToParent.append?.appendChild @
59
- addToParent.before?.parentNode.insertBefore @, addToParent.before
60
-
61
- jq.hide()
62
- setTimeout show, 0
63
- else
64
- show()
65
- @
66
-
67
- hide: (removeFromParent) ->
68
- $(@).hide 600, =>
69
- @parentNode?.removeChild @ if removeFromParent
70
- Batman.DOM.didRemoveNode(@)
71
- @
@@ -1,134 +0,0 @@
1
- applyExtra = (Batman) ->
2
-
3
- # `param` and `buildParams` stolen from jQuery
4
- #
5
- # jQuery JavaScript Library
6
- # http://jquery.com/
7
- #
8
- # Copyright 2011, John Resig
9
- # Dual licensed under the MIT or GPL Version 2 licenses.
10
- # http://jquery.org/license
11
- # Rails really doesn't like collection[0][rules], it wants collection[][rules],
12
- # so we will give it that.
13
- rbracket = /\[\]$/
14
- r20 = /%20/g
15
- param = (a) ->
16
- return a if typeof a is 'string'
17
- s = []
18
- add = (key, value) ->
19
- value = value() if typeof value is 'function'
20
- value = '' unless value?
21
- s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value)
22
-
23
- if Batman.typeOf(a) is 'Array'
24
- for value, name of a
25
- add name, value
26
- else
27
- for own k, v of a
28
- buildParams k, v, add
29
- s.join("&").replace r20, "+"
30
-
31
- buildParams = (prefix, obj, add) ->
32
- if Batman.typeOf(obj) is 'Array'
33
- for v, i in obj
34
- if rbracket.test(prefix)
35
- add prefix, v
36
- else
37
- buildParams prefix + "[]", v, add
38
- else if obj? and typeof obj == "object"
39
- for name of obj
40
- buildParams prefix + "[" + name + "]", obj[name], add
41
- else
42
- add prefix, obj
43
-
44
- numericKeys = [1, 2, 3, 4, 5, 6, 7, 10, 11]
45
- date_re = ///
46
- ^
47
- (\d{4}|[+\-]\d{6}) # 1 YYYY
48
- (?:-(\d{2}) # 2 MM
49
- (?:-(\d{2}))?)? # 3 DD
50
- (?:
51
- T(\d{2}): # 4 HH
52
- (\d{2}) # 5 mm
53
- (?::(\d{2}) # 6 ss
54
- (?:\.(\d{3}))?)? # 7 msec
55
- (?:(Z)| # 8 Z
56
- ([+\-]) # 9 ±
57
- (\d{2}) # 10 tzHH
58
- (?::(\d{2}))? # 11 tzmm
59
- )?
60
- )?
61
- $
62
- ///
63
-
64
- Batman.mixin Batman.Encoders,
65
- railsDate:
66
- defaultTimezoneOffset: (new Date()).getTimezoneOffset()
67
- encode: (value) -> value
68
- decode: (value) ->
69
- # Thanks to https://github.com/csnover/js-iso8601 for the majority of this algorithm.
70
- # MIT Licensed
71
- if value?
72
- if (obj = date_re.exec(value))
73
- # avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
74
- for key in numericKeys
75
- obj[key] = +obj[key] or 0
76
-
77
- # allow undefined days and months
78
- obj[2] = (+obj[2] || 1) - 1;
79
- obj[3] = +obj[3] || 1;
80
-
81
- # process timezone by adjusting minutes
82
- if obj[8] != "Z" and obj[9] != undefined
83
- minutesOffset = obj[10] * 60 + obj[11]
84
- minutesOffset = 0 - minutesOffset if obj[9] == "+"
85
- else
86
- minutesOffset = Batman.Encoders.railsDate.defaultTimezoneOffset
87
- return new Date(Date.UTC(obj[1], obj[2], obj[3], obj[4], obj[5] + minutesOffset, obj[6], obj[7]))
88
- else
89
- Batman.developer.warn "Unrecognized rails date #{value}!"
90
- return Date.parse(value)
91
-
92
- class Batman.RailsStorage extends Batman.RestStorage
93
-
94
- _serializeToFormData: (data) -> param(data)
95
-
96
- urlForRecord: -> @_addJsonExtension(super)
97
- urlForCollection: -> @_addJsonExtension(super)
98
-
99
- _addJsonExtension: (url) ->
100
- if url.indexOf('?') isnt -1 or url.substr(-5, 5) is '.json'
101
- return url
102
- url + '.json'
103
-
104
- _errorsFrom422Response: (response) -> JSON.parse(response)
105
-
106
- @::before 'update', 'create', (env, next) ->
107
- if @serializeAsForm && !Batman.Request.dataHasFileUploads(env.options.data)
108
- env.options.data = @_serializeToFormData(env.options.data)
109
- next()
110
-
111
- @::after 'update', 'create', ({error, record, response}, next) ->
112
- env = arguments[0]
113
- if error
114
- # Rails validation errors
115
- if error.request?.get('status') == 422
116
- try
117
- validationErrors = @_errorsFrom422Response(response)
118
- catch extractionError
119
- env.error = extractionError
120
- return next()
121
-
122
- for key, errorsArray of validationErrors
123
- for validationError in errorsArray
124
- record.get('errors').add(key, validationError)
125
-
126
- env.result = record
127
- env.error = record.get('errors')
128
- return next()
129
- next()
130
-
131
- if (module? && require?)
132
- module.exports = applyExtra
133
- else
134
- applyExtra(Batman)