-
Notifications
You must be signed in to change notification settings - Fork 44
/
breeze.ajaxpost.js
111 lines (99 loc) · 3.94 KB
/
breeze.ajaxpost.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//#region Copyright, Version, and Description
/*
* Functions to enable Breeze to use POST for queries when
* special parameters are passed using the .withParameters function.
*
* Copyright 2015 IdeaBlade, Inc. All Rights Reserved.
* Use, reproduction, distribution, and modification of this code is subject to the terms and
* conditions of the IdeaBlade Breeze license, available at http://www.breezejs.com/license
*
* Author: Steve Schmidt
* Version: 1.1.0 - revised: eliminated return object, configAjaxAdapter method; add ajaxPostEnabled flag
* 1.0.6 - original
*
* Special parameters:
* $method: ‘POST’ or ‘GET’ (the default)
* $encoding: ‘JSON’ or x-www-form-urlencoded (the default)
* $data: contains the data to be sent to the server
*
* Installation:
* play script after breeze
* call breeze.ajaxpost() if you change the ajax adapter (e.g, in Angular apps)
*
* Example:
* var query = breeze.EntityQuery.from('SimilarCustomersPOST')
* .withParameters({
* $method: 'POST',
* $encoding: 'JSON',
* $data: { CompanyName: 'Hilo Hattie', ContactName: 'Donald', City: 'Duck', Country: 'USA', Phone: '808-234-5678' }
* });
*
*/
//#endregion
(function (definition) {
if (typeof breeze === "object") {
definition(breeze);
} else if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node
var b = require('breeze-client');
definition(b);
} else if (typeof define === "function" && define["amd"]) {
// Requirejs / AMD
define(['breeze-client'], definition);
} else {
throw new Error("Can't find breeze");
}
}(function (breeze) {
'use strict';
breeze.config.ajaxpost = function(ajaxAdapter) {
wrapAjaxImpl(ajaxAdapter);
};
// For backwards compatibility, add to top level scope
breeze.ajaxpost = breeze.config.ajaxpost;
breeze.config.ajaxpost(); // immediately wrap whatever is the current ajax adapter
////////////////////
function wrapAjaxImpl(ajaxAdapter) {
if (!ajaxAdapter) {
ajaxAdapter = breeze.config.getAdapterInstance("ajax");
// There may not be a current ajax adapter. This can happen if breeze.base.*.js is being used
if (!ajaxAdapter) return;
}
if (ajaxAdapter.ajaxPostEnabled){
return; // already wrapped it.
}
var ajaxFunction = ajaxAdapter.ajax;
if (ajaxFunction) {
ajaxAdapter.ajax = function (settings) {
processSettings(settings);
return ajaxFunction.call(ajaxAdapter, settings);
};
ajaxAdapter.ajaxPostEnabled = true;
}
}
// Handle the POST-specific properties in the settings - $method, $data, $encoding
function processSettings(settings) {
var parameters = settings && settings.params;
if (!parameters) return settings;
// wrapped data; handle the special properties
settings.type = parameters.$method || settings.type; // GET is default method
var data = parameters.$data;
if (data) {
// if $data exists, assume all of other parameters are guidance for building a POST
if (parameters.$encoding === 'JSON') {
// JSON encoding
settings.processData = false; // don't let JQuery form-encode it
settings.contentType = "application/json; charset=UTF-8";
if (typeof (data) === 'object') {
settings.data = JSON.stringify(data); // encode parameters as JSON
} else {
settings.data = data;
}
} else {
settings.data = data;
}
// must be null or jQuery ajax adapter won't see settings.data
settings.params = null;
}
return settings;
}
}));