1 | (function() {
|
2 | var AjaxUtils, Collection, ajax_request,
|
3 | __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
4 |
|
5 | ajax_request = require("./ajax_request");
|
6 |
|
7 | AjaxUtils = require("./ajax_utils");
|
8 |
|
9 | Collection = (function() {
|
10 | function Collection(model) {
|
11 | this.model = model;
|
12 | this.failResponse = __bind(this.failResponse, this);
|
13 | this.recordsResponse = __bind(this.recordsResponse, this);
|
14 | if (typeof Visualforce !== "undefined") {
|
15 | ajax_request = require("./vf_request");
|
16 | }
|
17 | }
|
18 |
|
19 | Collection.prototype.find = function(id, params, options) {
|
20 | var record;
|
21 | if (options == null) {
|
22 | options = {};
|
23 | }
|
24 | record = new this.model({
|
25 | id: id
|
26 | });
|
27 | options.url = options.url || AjaxUtils.getURL(record);
|
28 | options.model = this.model;
|
29 | return ajax_request.queueRequest.get(params, options, this.model);
|
30 | };
|
31 |
|
32 | Collection.prototype.all = function(params, options) {
|
33 | if (options == null) {
|
34 | options = {};
|
35 | }
|
36 | if (!options.url) {
|
37 | options.url = this.model.url();
|
38 | }
|
39 | return ajax_request.queueRequest.get(params, options);
|
40 | };
|
41 |
|
42 | Collection.prototype.fetch = function(params, options) {
|
43 | var id, _this;
|
44 | if (params == null) {
|
45 | params = {};
|
46 | }
|
47 | if (options == null) {
|
48 | options = {};
|
49 | }
|
50 | if (id = params.id) {
|
51 | delete params.id;
|
52 | _this = this;
|
53 | this.find(id, params, options).end(function(err, res) {
|
54 | if (err) {
|
55 | return _this.failResponse(err, options);
|
56 | } else if (res.status >= 400) {
|
57 | return _this.failResponse(res.text, options);
|
58 | }
|
59 | _this.model.refresh(res.body, options);
|
60 | return _this.recordsResponse(res, options);
|
61 | });
|
62 | return true;
|
63 | } else {
|
64 | this.all(params, options).end((function(_this) {
|
65 | return function(err, res) {
|
66 | if (err) {
|
67 | return _this.failResponse(err, options);
|
68 | } else if (res.status >= 400) {
|
69 | return _this.failResponse(res.text, options);
|
70 | }
|
71 | _this.model.refresh(res.body, options);
|
72 | return _this.recordsResponse(res, options);
|
73 | };
|
74 | })(this));
|
75 | return true;
|
76 | }
|
77 | };
|
78 |
|
79 | Collection.prototype.recordsResponse = function(data, options) {
|
80 | var _ref;
|
81 | this.model.trigger('ajaxSuccess', data);
|
82 | return (_ref = options.done) != null ? _ref.apply(this.model, [data]) : void 0;
|
83 | };
|
84 |
|
85 | Collection.prototype.failResponse = function(error, options) {
|
86 | var _ref;
|
87 | this.model.trigger('ajaxError', error);
|
88 | return (_ref = options.fail) != null ? _ref.apply(this.model, [error]) : void 0;
|
89 | };
|
90 |
|
91 | return Collection;
|
92 |
|
93 | })();
|
94 |
|
95 | module.exports = Collection;
|
96 |
|
97 | }).call(this);
|