Sha256: e9e8b8531fe22b05668c0af5b0dbf42d5b72aded95ecccda65c9531af02d34d2

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

# a simple reimplentation
# to use traffic cop
methodMap = {
    'create': 'POST',
    'update': 'PUT',
    'patch':  'PATCH',
    'delete': 'DELETE',
    'read':   'GET'
}

getValue = (object, prop)->
    if !(object && object[prop])
        return null;
    return if _.isFunction(object[prop]) then object[prop]() else object[prop];


urlError = ->
    throw new Error('A "url" property or function must be specified')


paramsMap = {
    fields  : 'f'
    with    : 'w'
    query   : 'q'
    include : 'i'
    order   : 'o'
    limit   : 'l'
    start   : 's'
}

Lanes.Data.Sync = (method, model, options)->

    query = {}
    for key, value of options
        query[ paramsMap[key] ] = value if paramsMap[key]

    # Default JSON-request options.
    params = {
        type: methodMap[method]
        dataType: "json"
        data: query
    }

    # Ensure that we have a URL.
    params.url = _.result(model, "url") or urlError() unless options.url
    params.url += '.json'
    params.headers = {
        X_CSRF_TOKEN: Lanes.Data.Config.csrf_token
    }
    # Ensure that we have the appropriate request data.
    if !options.data? && model && _.contains([ "create", "update", "patch" ], method )
        params.contentType = "application/json"
        params.data = JSON.stringify( model.dataForSave(options) )

    # Don't process data on a non-GET request.
    #params.processData = false if params.type isnt "GET"

    # Make the request, allowing the user to override any Ajax options.
    xhr = options.xhr = Lanes.$.ajax(_.extend(params, options))
    model.trigger "request", model, xhr, options
    xhr

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lanes-0.0.3 client/javascripts/data/Sync.coffee
lanes-0.0.2 client/javascripts/data/Sync.coffee
lanes-0.0.1 client/javascripts/data/Sync.coffee