UNPKG

4.26 kBJavaScriptView Raw
1(function() {
2 var AjaxUtils, Singleton, ajax_request, _3Model,
3 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
4
5 _3Model = require("3vot-model");
6
7 ajax_request = require("./ajax_request");
8
9 AjaxUtils = require("./ajax_utils");
10
11 Singleton = (function() {
12 function Singleton(record) {
13 this.record = record;
14 this.failResponse = __bind(this.failResponse, this);
15 this.recordResponse = __bind(this.recordResponse, this);
16 this.model = this.record.constructor;
17 if (typeof Visualforce !== "undefined") {
18 ajax_request = require("./vf_request");
19 }
20 }
21
22 Singleton.prototype.reload = function(params, options) {
23 if (options == null) {
24 options = {};
25 }
26 params.data = this.record.toJSON();
27 options.url = options.url || AjaxUtils.getURL(this.record);
28 options.error = this.failResponse;
29 options.record = this.record;
30 return ajax_request.queueRequest.get(params, options).end((function(_this) {
31 return function(res) {
32 if (err) {
33 return _this.failResponse(err, options);
34 } else if (res.status >= 400) {
35 return _this.failResponse(res.text, options);
36 }
37 return _this.recordResponse(res.body, options);
38 };
39 })(this));
40 };
41
42 Singleton.prototype.create = function(params, options) {
43 if (options == null) {
44 options = {};
45 }
46 params.data = this.record.toJSON();
47 options.url = options.url || AjaxUtils.getCollectionURL(this.record);
48 return ajax_request.queueRequest.post(params, options).end((function(_this) {
49 return function(err, res) {
50 if (err) {
51 return _this.failResponse(err, options);
52 } else if (res.status >= 400) {
53 return _this.failResponse(res.text, options);
54 }
55 return _this.recordResponse(res.body, options);
56 };
57 })(this));
58 };
59
60 Singleton.prototype.update = function(params, options) {
61 if (options == null) {
62 options = {};
63 }
64 params.data = this.record.toJSON();
65 options.url = options.url || AjaxUtils.getURL(this.record);
66 return ajax_request.queueRequest.put(params, options).end((function(_this) {
67 return function(err, res) {
68 if (err) {
69 return _this.failResponse(err, options);
70 } else if (res.status >= 400) {
71 return _this.failResponse(res.text, options);
72 }
73 return _this.recordResponse(res.body, options);
74 };
75 })(this));
76 };
77
78 Singleton.prototype.destroy = function(params, options) {
79 if (options == null) {
80 options = {};
81 }
82 params.data = this.record.toJSON();
83 options.url = options.url || AjaxUtils.getURL(this.record);
84 options.error = this.failResponse;
85 return ajax_request.queueRequest.del(params, options).end((function(_this) {
86 return function(err, res) {
87 if (err) {
88 return _this.failResponse(err, options);
89 } else if (res.status >= 400) {
90 return _this.failResponse(res.text, options);
91 }
92 return _this.recordResponse(res.body, options);
93 };
94 })(this));
95 };
96
97 Singleton.prototype.recordResponse = function(data, options) {
98 var _ref;
99 if (options == null) {
100 options = {};
101 }
102 ajax_request.disable((function(_this) {
103 return function() {
104 if (!(_3Model.isBlank(data) || _this.record.destroyed)) {
105 if (data.id && _this.record.id !== data.id) {
106 _this.record.changeID(data.id);
107 }
108 return _this.record.refresh(data);
109 }
110 };
111 })(this));
112 this.record.trigger('ajaxSuccess', data);
113 return (_ref = options.done) != null ? _ref.apply(this.record) : void 0;
114 };
115
116 Singleton.prototype.failResponse = function(error, options) {
117 var _ref;
118 if (options == null) {
119 options = {};
120 }
121 this.record.trigger('ajaxError', error);
122 return (_ref = options.fail) != null ? _ref.apply(this.record, [error]) : void 0;
123 };
124
125 return Singleton;
126
127 })();
128
129 module.exports = Singleton;
130
131}).call(this);