From 3034e27fce49b2b0f956a42ed32f35046f8a0c42 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 20 Apr 2018 11:03:50 +0100 Subject: [PATCH] args fix --- README.md | 17 +++++++++++++++++ firebase.coffee | 21 ++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index f38c055..e7afb40 100755 --- a/README.md +++ b/README.md @@ -198,11 +198,21 @@ Retrieves data from the database. firebase.get "/value", (value) -> print value +# Promise +firebase.get "/value" +.then (value) -> print value + # Simple 2, expecting dataset firebase.get "/names", (names) -> namesArray = _.toArray(names) # converts JSON to array print name for name in namesArray +# Promise +firebase.get "/names" +.then (names) -> + namesArray = _.toArray(names) # converts JSON to array + print name for name in namesArray + # Advanced response = (names) -> namesArray = _.toArray(names) @@ -210,6 +220,10 @@ response = (names) -> firebase.get("/names",response,{orderBy: "$key", limitToFirst: 5}) +# Promise +firebase.get("/names",{orderBy: "$key", limitToFirst: 5}) +.then(response) + ```
@@ -236,6 +250,9 @@ response = (confirmation) -> firebase.put("/values", {"foo": true, "bar": false}, response) +# Promise +firebase.put("/values", {"foo": true, "bar": false}) +.then(response) ```
diff --git a/firebase.coffee b/firebase.coffee index 2f83955..0313bc9 100644 --- a/firebase.coffee +++ b/firebase.coffee @@ -1,5 +1,3 @@ - - # Documentation of this Module: https://github.com/marckrenn/framer-Firebase # ------ : ------- Firebase REST API: https://firebase.google.com/docs/reference/rest/database/ @@ -23,12 +21,10 @@ class exports.Firebase extends Framer.BaseClass console.log "Firebase: Connecting to Firebase Project '#{@projectID}' ... \n URL: 'https://#{@projectID}.firebaseio.com'" if @debug @.onChange "connection" - request = (project, secret, path, callback, method, data, parameters, debug) -> url = "https://#{project}.firebaseio.com#{path}.json#{secret}" - if parameters? if parameters.shallow then url += "&shallow=true" if parameters.format is "export" then url += "&format=export" @@ -66,14 +62,21 @@ class exports.Firebase extends Framer.BaseClass return r + # Third argument can also accept options, rather than callback + parseArgs = (args..., cb) -> + if typeof args[2] is "object" + args[3] = args[2] + args[2] = null + + return cb.apply(null, args) # Available methods - get: (path, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "GET", null, parameters, @debug) - put: (path, data, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "PUT", data, parameters, @debug) - post: (path, data, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "POST", data, parameters, @debug) - patch: (path, data, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "PATCH", data, parameters, @debug) - delete: (path, callback, parameters) -> request(@projectID, @secretEndPoint, path, callback, "DELETE", null, parameters, @debug) + get: (args...) -> parseArgs args..., (path, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "GET", null, parameters, @debug) + put: (args...) -> parseArgs args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "PUT", data, parameters, @debug) + post: (args...) -> parseArgs args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "POST", data, parameters, @debug) + patch: (args...) -> parseArgs args..., (path, data, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "PATCH", data, parameters, @debug) + delete: (args...) -> parseArgs args..., (path, callback, parameters) => request(@projectID, @secretEndPoint, path, callback, "DELETE", null, parameters, @debug) onChange: (path, callback) ->