1 | (function() {
|
2 | var Action, ajax_request,
|
3 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
4 |
|
5 | ajax_request = require("./ajax_request");
|
6 |
|
7 | Action = (function() {
|
8 | function Action(model) {
|
9 | this.model = model;
|
10 | this.failResponse = __bind(this.failResponse, this);
|
11 | this.recordsResponse = __bind(this.recordsResponse, this);
|
12 | }
|
13 |
|
14 | Action.prototype.destroy = function(values, options) {
|
15 | var params;
|
16 | if (values == null) {
|
17 | values = {};
|
18 | }
|
19 | if (options == null) {
|
20 | options = {};
|
21 | }
|
22 | options.url = this.model.url();
|
23 | params = {
|
24 | query: values
|
25 | };
|
26 | ajax_request.queueRequest.del(params, options).end((function(_this) {
|
27 | return function(err, res) {
|
28 | if (err) {
|
29 | return _this.failResponse(err, options);
|
30 | } else if (res.status >= 400) {
|
31 | return _this.failResponse(res.text, options);
|
32 | }
|
33 | return _this.recordsResponse(res.body, options);
|
34 | };
|
35 | })(this));
|
36 | return true;
|
37 | };
|
38 |
|
39 | Action.prototype.call = function(name, values, options) {
|
40 | var params;
|
41 | if (values == null) {
|
42 | values = {};
|
43 | }
|
44 | if (options == null) {
|
45 | options = {};
|
46 | }
|
47 | options.url = this.model.url() + "/actions/" + name;
|
48 | params = {
|
49 | data: values
|
50 | };
|
51 | ajax_request.queueRequest.post(params, options).end((function(_this) {
|
52 | return function(err, res) {
|
53 | if (err) {
|
54 | return _this.failResponse(err, options);
|
55 | } else if (res.status >= 400) {
|
56 | return _this.failResponse(res.text, options);
|
57 | }
|
58 | return _this.recordsResponse(res.body, options);
|
59 | };
|
60 | })(this));
|
61 | return true;
|
62 | };
|
63 |
|
64 | Action.prototype.recordsResponse = function(data, options) {
|
65 | var _ref;
|
66 | this.model.trigger('ajaxSuccess', data);
|
67 | this.model.trigger('actionSuccess', data);
|
68 | return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0;
|
69 | };
|
70 |
|
71 | Action.prototype.failResponse = function(error, options) {
|
72 | var _ref;
|
73 | this.model.trigger('ajaxError', error);
|
74 | this.model.trigger('actionError', error);
|
75 | return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0;
|
76 | };
|
77 |
|
78 | return Action;
|
79 |
|
80 | })();
|
81 |
|
82 | module.exports = Action;
|
83 |
|
84 | }).call(this);
|