Skip to content

Instantly share code, notes, and snippets.

@jfeldstein
Last active December 30, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfeldstein/7793905 to your computer and use it in GitHub Desktop.
Save jfeldstein/7793905 to your computer and use it in GitHub Desktop.
List of gotchas for dealing with "bugs" in Parse's javascript SDK
## Fetch related when fetching
fetch: (options) ->
console.log("Fetching order")
# Does not work when order is created by rotateOrder, and does not have an .id specified.
(OrderModel.__super__.fetch.apply(this, [])).then =>
@fetch_related().then ->
options.success() if options.success?
## Fetch related, useing deffered and returning promises
fetch_related: ->
fetch_rest = $.Deferred (defer) =>
@get_restaurant().fetch deferredHandlers(defer)
fetch_addr = $.Deferred (defer) =>
@get_delivery_address().fetch deferredHandlers(defer)
fetch_cc = $.Deferred (defer) =>
@get_billing_cc().fetch deferredHandlers(defer)
$.when fetch_rest, fetch_addr, fetch_cc
## Constructor of getter for related model that return an instance
## of domain-specific classes, instead of native Parse object
buildRelatedGetter = (relation, class_name) ->
->
if @get(relation)? and !(@get(relation) instanceof App.Models[class_name])
# Convert related to class
attrs = @get(relation).attributes
attrs.objectId = @get(relation).id
@set relation, new App.Models[class_name](attrs)
@get relation
## Extending Parse.User requires a workaround:
_.extend Parse.User.prototype,
defaults: {...}
getter: ->
# ...
setter: (val) ->
# ...
## Persisted anonymous Parse users:
getUserForAppState = ->
# Fetch related compenents to get started with user.
current_user = Parse.User.current()
# First time in app? Create new random user.
if !current_user?
current_user = new Parse.User
username: randomString 40
password: randomString 40
# Save the new random user.
# TODO: Figure out some way to handle errors when creating these anonymous users.
current_user.signUp
error: ->
console.error "FAILURE FETCHING USER", arguments
current_user
## Get a single config variable hosted on Parse
window.getConfig = (config_name, cb) ->
configError = (object, error) ->
console.error("COULDN'T RETRIEVE CONFIG VAR: "+config_name, error)
cb error.code
query = new Parse.Query(pizzabuttonapp.Models.ConfigModel)
query.equalTo 'name', config_name
query.find
success: (result) ->
config = result[0]
if config?
cb null, config.get('value')
else
cb
code: "Config with that name not found."
error: configError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment