Skip to content

Commit ea2bfb8

Browse files
committed
first commit
1 parent d733719 commit ea2bfb8

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed

README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1-
# parse-js-sdk-iced-coffeescript-patch
2-
A small patch that adds IcedCoffeeScript style deferral to Parse JS SDK
1+
# Parse JS SDK IcedCoffeeScript Patch
2+
A small patch that adds IcedCoffeeScript style deferral to Parse JS SDK.
3+
4+
## Usage
5+
Compile `parse-iced-patch.iced` with IcedCoffeeScript compiler and include it after including Parse JS SDK.
6+
You can use it in both Parse Cloud Code and browser client.
7+
8+
It wraps Parse JS SDK methods and generate `X` methods.
9+
For example, to use `Parse.Object.saveAll()` with Iced syntax, you call `Parse.Object.saveAllX()`.
10+
11+
## Convertion
12+
Conside this sample code which adds 100 to the field `stock` to all `Product` rows that having `stock == 0`:
13+
### Original Code
14+
```coffee
15+
productQuery = new Parse.Query 'Product'
16+
productQuery.equalTo 'stock', 0
17+
productQuery.find
18+
success: (products) ->
19+
for product in products
20+
product.increment 'stock', 100
21+
Parse.Object.saveAll products,
22+
success: ->
23+
console.log 'Completed!'
24+
error: ->
25+
console.log 'Error!'
26+
error: (error) ->
27+
console.log 'Error!'
28+
```
29+
### After Convertion
30+
```coffee
31+
productQuery = new Parse.Query 'Product'
32+
productQuery.equalTo 'stock', 0
33+
34+
await productQuery.findX defer products, error
35+
return console.log 'Error!' if error?
36+
37+
for product in products
38+
product.increment 'stock', 100
39+
40+
await Parse.Object.saveAllX products, defer result, error
41+
return console.log 'Error!' if error?
42+
43+
console.log 'Completed!'
44+
```
45+
46+
## Methods Wrapped
47+
* Parse.Query.prototype
48+
* count
49+
* each
50+
* find
51+
* first
52+
* get
53+
54+
* Parse.Object.prototype
55+
* destroy
56+
* fetch
57+
* save
58+
59+
* Parse.Object
60+
* destroyAll
61+
* saveAll

parse-iced-patch.iced

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
( ->
2+
3+
promiseToIcedDeferral = (object, funcName) ->
4+
overridenFunc = object[funcName]
5+
newFunc = ->
6+
args = [].slice.call arguments
7+
cb = args.pop()
8+
promise = overridenFunc.apply this, args
9+
promise.then (result) ->
10+
cb result, undefined
11+
, (error) ->
12+
cb undefined, error
13+
object[funcName+'X'] = newFunc
14+
15+
optionsToIcedDeferral = (object, funcName) ->
16+
overridenFunc = object[funcName]
17+
newFunc = ->
18+
args = [].slice.call arguments
19+
cb = args.pop()
20+
21+
options = undefined
22+
23+
if args.length > 0
24+
options = args.pop()
25+
if options is undefined or options.constructor isnt Object
26+
# not an option hash, push it back
27+
args.push options
28+
options = undefined
29+
30+
options = {} if options is undefined
31+
options.success = (result) ->
32+
cb result, undefined
33+
options.error = (error) ->
34+
cb undefined, error
35+
36+
args.push options
37+
overridenFunc.apply this, args
38+
object[funcName+'X'] = newFunc
39+
40+
41+
['count', 'each', 'find', 'first'].forEach (funcName) ->
42+
promiseToIcedDeferral Parse.Query.prototype, funcName
43+
44+
['destroy', 'fetch', 'save'].forEach (funcName) ->
45+
promiseToIcedDeferral Parse.Object.prototype, funcName
46+
47+
['destroyAll'].forEach (funcName) ->
48+
promiseToIcedDeferral Parse.Object, funcName
49+
50+
['get'].forEach (funcName) ->
51+
optionsToIcedDeferral Parse.Query.prototype, funcName
52+
53+
['saveAll'].forEach (funcName) ->
54+
optionsToIcedDeferral Parse.Object, funcName
55+
56+
)()

0 commit comments

Comments
 (0)