github.com/machinebox/remoto@v0.1.2-0.20191024144331-eff21a7d321f/templates/remotohttp/client.jquery.js.plush (about)

     1  (function($){
     2  	$.<%= def.PackageName %> = $.<%= def.PackageName %> || {}
     3  	<%= for (service) in def.Services { %>
     4  	// $.<%= def.PackageName %>.<%= service.Name %>Client is a client capable of
     5  	// interacting with <%= service.Name %> services.
     6  	// Usage:
     7  	// 	var client = new $.<%= def.PackageName %>.<%= service.Name %>Client({
     8  	// 		endpoint: "http://localhost:8080"
     9  	// 	})
    10  	$.<%= def.PackageName %>.<%= service.Name %>Client = function(options){
    11  		this.options = this.options || {}
    12  		this.options.endpoint = this.options.endpoint || "http://localhost:8080"
    13  	}
    14  	<%= for (method) in service.Methods { %><%= print_comment(method.Comment) %><%= if (method.RequestStructure.HasFields) { %>
    15  	// Must pass in an instance of $.<%= def.PackageName %>.<%= method.RequestStructure.Name %>.
    16  	$.<%= def.PackageName %>.<%= service.Name %>Client.prototype.<%= method.Name %> = function(<%= camelize_down(method.RequestStructure.Name) %>) {
    17  		return this.<%= method.Name %>Multi([<%= camelize_down(method.RequestStructure.Name) %>])
    18  			.then(function(responses){
    19  				return responses[0]
    20  			})
    21  	}
    22  	
    23  	// $.<%= def.PackageName %>.<%= service.Name %>Client.prototype.<%= method.Name %>Multi is a batch
    24  	// version of $.<%= def.PackageName %>.<%= service.Name %>Client.prototype.<%= method.Name %>.
    25  	// Pass in an array of request objects, and get back an array of response objects.
    26  	$.<%= def.PackageName %>.<%= service.Name %>Client.prototype.<%= method.Name %>Multi = function(<%= camelize_down(method.RequestStructure.Name) %>s) {
    27  		<%= if (method.ResponseStructure.Name == "remototypes.FileResponse") { %>
    28  		if (<%= camelize_down(method.RequestStructure.Name) %>s.length > 1) {
    29  			throw '$.<%= def.PackageName %>.<%= service.Name %>Client.<%= method.Name %>Multi: batch requests are not supported for file responses, use $.<%= def.PackageName %>.<%= service.Name %>Client.<%= method.Name %> instead.'
    30  		}<% } %>var filesCount = 0
    31  		var formData = new FormData()
    32  		requests.forEach(function(request){
    33  			request._files.forEach(function(file) {
    34  				formData.append(file, request._files[file])
    35  			})
    36  		})
    37  		formData.append('json', JSON.stringify(requests))
    38  		return $.ajax({
    39  			method: 'post', url: this.options.endpoint + '/remoto/<%= service.Name %>.<%= method.Name %>',
    40  			data: formData,
    41  			contentType: false,
    42  			processData: false
    43  		}).then(function(responses) {
    44  			var responseObjects = []
    45  			responses.forEach(function(response){
    46  				responseObjects.push(new $.<%= def.PackageName %>.<%= method.ResponseStructure.Name %>(response))
    47  			})
    48  			return responseObjects
    49  		})
    50  	}
    51  	<% } %><% } %>
    52  	<%= for (structure) in unique_structures(def) { %>
    53  	<%= print_comment(structure.Comment) %>$.<%= def.PackageName %>.<%= structure.Name %> = function(data) {
    54  		this.data = data
    55  	}
    56  	$.<%= def.PackageName %>.<%= structure.Name %>.prototype.toJSON = function() {
    57  		return JSON.stringify(this.data)
    58  	}
    59  	<%= if (structure.IsRequestObject) { %>
    60  	// _addFile adds a file to the request.
    61  	$.<%= def.PackageName %>.<%= structure.Name %>.prototype._addFile = function(file) {
    62  		this._files = this._files || {}
    63  		var fieldname = 'files['+$.<%= def.PackageName %>._filesCount+']'
    64  		this._files[fieldname] = file
    65  		$.<%= def.PackageName %>._filesCount++
    66  		return fieldname
    67  	}
    68  	<% } %>
    69  	<%= for (field) in structure.Fields { %>
    70  	// get<%= field.Name %> gets the <%= camelize_down(field.Name) %> from this object.
    71  	$.<%= def.PackageName %>.<%= structure.Name %>.prototype.get<%= field.Name %> = function() {
    72  		return this.data.<%= camelize_down(field.Name) %>
    73  	}
    74  	<%= if (!structure.IsResponseObject) { %>
    75  	<%= if (field.Type.Name == "remototypes.File") { %>
    76  	// set<%= field.Name %> sets the <%= camelize_down(field.Name) %> on this object.
    77  	// The root request must also be provided so it can be informed of the file.
    78  	$.<%= def.PackageName %>.<%= structure.Name %>.prototype.set<%= field.Name %> = function(request, <%= camelize_down(field.Name) %>) {
    79  		this.data.<%= camelize_down(field.Name) %> = request._addFile(<%= camelize_down(field.Name) %>)
    80  	}
    81  	<% } else { %>
    82  	// set<%= field.Name %> sets the <%= camelize_down(field.Name) %> on this object.
    83  	$.<%= def.PackageName %>.<%= structure.Name %>.prototype.set<%= field.Name %> = function(<%= camelize_down(field.Name) %>) {
    84  		this.data.<%= camelize_down(field.Name) %> = <%= camelize_down(field.Name) %>
    85  	}<% } %><% } %>
    86  	<% } %><% } %><% } %>
    87  	// _filesCount keeps track of the number of files being added, and is used
    88  	// to generate unique field names.
    89  	$.<%= def.PackageName %>._filesCount = 0
    90  
    91  })(jQuery)