spine-rails 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,16 @@
1
1
  (function() {
2
- var $, Ajax, Base, Collection, Extend, Include, Model, Singleton;
3
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5
- function ctor() { this.constructor = child; }
6
- ctor.prototype = parent.prototype;
7
- child.prototype = new ctor;
8
- child.__super__ = parent.prototype;
9
- return child;
10
- };
11
- if (typeof Spine === "undefined" || Spine === null) {
12
- Spine = require('spine');
13
- }
2
+ var $, Ajax, Base, Collection, Extend, Include, Model, Singleton,
3
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
4
+ __hasProp = Object.prototype.hasOwnProperty,
5
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; },
6
+ __slice = Array.prototype.slice;
7
+
8
+ if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
9
+
14
10
  $ = Spine.$;
11
+
15
12
  Model = Spine.Model;
13
+
16
14
  Ajax = {
17
15
  getURL: function(object) {
18
16
  return object && (typeof object.url === "function" ? object.url() : void 0) || object.url;
@@ -21,9 +19,13 @@
21
19
  pending: false,
22
20
  requests: [],
23
21
  disable: function(callback) {
24
- this.enabled = false;
25
- callback();
26
- return this.enabled = true;
22
+ if (this.enabled) {
23
+ this.enabled = false;
24
+ callback();
25
+ return this.enabled = true;
26
+ } else {
27
+ return callback();
28
+ }
27
29
  },
28
30
  requestNext: function() {
29
31
  var next;
@@ -35,14 +37,13 @@
35
37
  }
36
38
  },
37
39
  request: function(callback) {
38
- return (callback()).complete(__bind(function() {
39
- return this.requestNext();
40
- }, this));
40
+ var _this = this;
41
+ return (callback()).complete(function() {
42
+ return _this.requestNext();
43
+ });
41
44
  },
42
45
  queue: function(callback) {
43
- if (!this.enabled) {
44
- return;
45
- }
46
+ if (!this.enabled) return;
46
47
  if (this.pending) {
47
48
  this.requests.push(callback);
48
49
  } else {
@@ -52,8 +53,11 @@
52
53
  return callback;
53
54
  }
54
55
  };
56
+
55
57
  Base = (function() {
58
+
56
59
  function Base() {}
60
+
57
61
  Base.prototype.defaults = {
58
62
  contentType: 'application/json',
59
63
  dataType: 'json',
@@ -62,21 +66,29 @@
62
66
  'X-Requested-With': 'XMLHttpRequest'
63
67
  }
64
68
  };
69
+
65
70
  Base.prototype.ajax = function(params, defaults) {
66
71
  return $.ajax($.extend({}, this.defaults, defaults, params));
67
72
  };
73
+
68
74
  Base.prototype.queue = function(callback) {
69
75
  return Ajax.queue(callback);
70
76
  };
77
+
71
78
  return Base;
79
+
72
80
  })();
73
- Collection = (function() {
74
- __extends(Collection, Base);
81
+
82
+ Collection = (function(_super) {
83
+
84
+ __extends(Collection, _super);
85
+
75
86
  function Collection(model) {
76
87
  this.model = model;
77
88
  this.errorResponse = __bind(this.errorResponse, this);
78
89
  this.recordsResponse = __bind(this.recordsResponse, this);
79
90
  }
91
+
80
92
  Collection.prototype.find = function(id, params) {
81
93
  var record;
82
94
  record = new this.model({
@@ -87,123 +99,163 @@
87
99
  url: Ajax.getURL(record)
88
100
  }).success(this.recordsResponse).error(this.errorResponse);
89
101
  };
102
+
90
103
  Collection.prototype.all = function(params) {
91
104
  return this.ajax(params, {
92
105
  type: 'GET',
93
106
  url: Ajax.getURL(this.model)
94
107
  }).success(this.recordsResponse).error(this.errorResponse);
95
108
  };
96
- Collection.prototype.fetch = function(params) {
97
- var id;
98
- if (params == null) {
99
- params = {};
100
- }
109
+
110
+ Collection.prototype.fetch = function(params, options) {
111
+ var id,
112
+ _this = this;
113
+ if (params == null) params = {};
114
+ if (options == null) options = {};
101
115
  if (id = params.id) {
102
116
  delete params.id;
103
- return this.find(id, params).success(__bind(function(record) {
104
- return this.model.refresh(record);
105
- }, this));
117
+ return this.find(id, params).success(function(record) {
118
+ return _this.model.refresh(record, options);
119
+ });
106
120
  } else {
107
- return this.all(params).success(__bind(function(records) {
108
- return this.model.refresh(records);
109
- }, this));
121
+ return this.all(params).success(function(records) {
122
+ return _this.model.refresh(records, options);
123
+ });
110
124
  }
111
125
  };
126
+
112
127
  Collection.prototype.recordsResponse = function(data, status, xhr) {
113
128
  return this.model.trigger('ajaxSuccess', null, status, xhr);
114
129
  };
130
+
115
131
  Collection.prototype.errorResponse = function(xhr, statusText, error) {
116
132
  return this.model.trigger('ajaxError', null, xhr, statusText, error);
117
133
  };
134
+
118
135
  return Collection;
119
- })();
120
- Singleton = (function() {
121
- __extends(Singleton, Base);
136
+
137
+ })(Base);
138
+
139
+ Singleton = (function(_super) {
140
+
141
+ __extends(Singleton, _super);
142
+
122
143
  function Singleton(record) {
123
144
  this.record = record;
124
145
  this.errorResponse = __bind(this.errorResponse, this);
125
- this.blankResponse = __bind(this.blankResponse, this);
126
146
  this.recordResponse = __bind(this.recordResponse, this);
127
147
  this.model = this.record.constructor;
128
148
  }
129
- Singleton.prototype.reload = function(params) {
130
- return this.queue(__bind(function() {
131
- return this.ajax(params, {
149
+
150
+ Singleton.prototype.reload = function(params, options) {
151
+ var _this = this;
152
+ return this.queue(function() {
153
+ return _this.ajax(params, {
132
154
  type: 'GET',
133
- url: Ajax.getURL(this.record)
134
- }).success(this.recordResponse).error(this.errorResponse);
135
- }, this));
155
+ url: Ajax.getURL(_this.record)
156
+ }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
157
+ });
136
158
  };
137
- Singleton.prototype.create = function(params) {
138
- return this.queue(__bind(function() {
139
- return this.ajax(params, {
159
+
160
+ Singleton.prototype.create = function(params, options) {
161
+ var _this = this;
162
+ return this.queue(function() {
163
+ return _this.ajax(params, {
140
164
  type: 'POST',
141
- data: JSON.stringify(this.record),
142
- url: Ajax.getURL(this.model)
143
- }).success(this.recordResponse).error(this.errorResponse);
144
- }, this));
165
+ data: JSON.stringify(_this.record),
166
+ url: Ajax.getURL(_this.model)
167
+ }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
168
+ });
145
169
  };
146
- Singleton.prototype.update = function(params) {
147
- return this.queue(__bind(function() {
148
- return this.ajax(params, {
170
+
171
+ Singleton.prototype.update = function(params, options) {
172
+ var _this = this;
173
+ return this.queue(function() {
174
+ return _this.ajax(params, {
149
175
  type: 'PUT',
150
- data: JSON.stringify(this.record),
151
- url: Ajax.getURL(this.record)
152
- }).success(this.recordResponse).error(this.errorResponse);
153
- }, this));
176
+ data: JSON.stringify(_this.record),
177
+ url: Ajax.getURL(_this.record)
178
+ }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
179
+ });
154
180
  };
155
- Singleton.prototype.destroy = function(params) {
156
- return this.queue(__bind(function() {
157
- return this.ajax(params, {
181
+
182
+ Singleton.prototype.destroy = function(params, options) {
183
+ var _this = this;
184
+ return this.queue(function() {
185
+ return _this.ajax(params, {
158
186
  type: 'DELETE',
159
- url: Ajax.getURL(this.record)
160
- }).success(this.recordResponse).error(this.errorResponse);
161
- }, this));
187
+ url: Ajax.getURL(_this.record)
188
+ }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
189
+ });
162
190
  };
163
- Singleton.prototype.recordResponse = function(data, status, xhr) {
164
- this.record.trigger('ajaxSuccess', status, xhr);
165
- if (Spine.isBlank(data)) {
166
- return;
167
- }
168
- data = this.model.fromJSON(data);
169
- return Ajax.disable(__bind(function() {
170
- if (data.id && this.record.id !== data.id) {
171
- this.record.changeID(data.id);
191
+
192
+ Singleton.prototype.recordResponse = function(options) {
193
+ var _this = this;
194
+ if (options == null) options = {};
195
+ return function(data, status, xhr) {
196
+ var _ref;
197
+ if (Spine.isBlank(data)) {
198
+ data = false;
199
+ } else {
200
+ data = _this.model.fromJSON(data);
172
201
  }
173
- return this.record.updateAttributes(data.attributes());
174
- }, this));
202
+ Ajax.disable(function() {
203
+ if (data) {
204
+ if (data.id && _this.record.id !== data.id) {
205
+ _this.record.changeID(data.id);
206
+ }
207
+ return _this.record.updateAttributes(data.attributes());
208
+ }
209
+ });
210
+ _this.record.trigger('ajaxSuccess', data, status, xhr);
211
+ return (_ref = options.success) != null ? _ref.apply(_this.record) : void 0;
212
+ };
175
213
  };
176
- Singleton.prototype.blankResponse = function(data, status, xhr) {
177
- return this.record.trigger('ajaxSuccess', status, xhr);
178
- };
179
- Singleton.prototype.errorResponse = function(xhr, statusText, error) {
180
- return this.record.trigger('ajaxError', xhr, statusText, error);
214
+
215
+ Singleton.prototype.errorResponse = function(options) {
216
+ var _this = this;
217
+ if (options == null) options = {};
218
+ return function(xhr, statusText, error) {
219
+ var _ref;
220
+ _this.record.trigger('ajaxError', xhr, statusText, error);
221
+ return (_ref = options.error) != null ? _ref.apply(_this.record) : void 0;
222
+ };
181
223
  };
224
+
182
225
  return Singleton;
183
- })();
226
+
227
+ })(Base);
228
+
184
229
  Model.host = '';
230
+
185
231
  Include = {
186
232
  ajax: function() {
187
233
  return new Singleton(this);
188
234
  },
189
235
  url: function() {
190
- var base;
191
- base = Ajax.getURL(this.constructor);
192
- if (base.charAt(base.length - 1) !== '/') {
193
- base += '/';
194
- }
195
- base += encodeURIComponent(this.id);
196
- return base;
236
+ var args, url;
237
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
238
+ url = Ajax.getURL(this.constructor);
239
+ if (url.charAt(url.length - 1) !== '/') url += '/';
240
+ url += encodeURIComponent(this.id);
241
+ args.unshift(url);
242
+ return args.join('/');
197
243
  }
198
244
  };
245
+
199
246
  Extend = {
200
247
  ajax: function() {
201
248
  return new Collection(this);
202
249
  },
203
250
  url: function() {
204
- return "" + Model.host + "/" + (this.className.toLowerCase()) + "s";
251
+ var args;
252
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
253
+ args.unshift(this.className.toLowerCase() + 's');
254
+ args.unshift(Model.host);
255
+ return args.join('/');
205
256
  }
206
257
  };
258
+
207
259
  Model.Ajax = {
208
260
  extended: function() {
209
261
  this.fetch(this.ajaxFetch);
@@ -215,18 +267,24 @@
215
267
  var _ref;
216
268
  return (_ref = this.ajax()).fetch.apply(_ref, arguments);
217
269
  },
218
- ajaxChange: function(record, type) {
219
- return record.ajax()[type]();
270
+ ajaxChange: function(record, type, options) {
271
+ if (options == null) options = {};
272
+ if (options.ajax === false) return;
273
+ return record.ajax()[type](options.ajax, options);
220
274
  }
221
275
  };
276
+
222
277
  Model.Ajax.Methods = {
223
278
  extended: function() {
224
279
  this.extend(Extend);
225
280
  return this.include(Include);
226
281
  }
227
282
  };
283
+
284
+ Ajax.defaults = Base.prototype.defaults;
285
+
228
286
  Spine.Ajax = Ajax;
229
- if (typeof module !== "undefined" && module !== null) {
230
- module.exports = Ajax;
231
- }
287
+
288
+ if (typeof module !== "undefined" && module !== null) module.exports = Ajax;
289
+
232
290
  }).call(this);
@@ -1,42 +1,44 @@
1
1
  (function() {
2
- var $;
3
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
4
- for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
5
- function ctor() { this.constructor = child; }
6
- ctor.prototype = parent.prototype;
7
- child.prototype = new ctor;
8
- child.__super__ = parent.prototype;
9
- return child;
10
- };
11
- if (typeof Spine === "undefined" || Spine === null) {
12
- Spine = require('spine');
13
- }
2
+ var $,
3
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
4
+ __hasProp = Object.prototype.hasOwnProperty,
5
+ __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
6
+
7
+ if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
8
+
14
9
  $ = Spine.$;
15
- Spine.List = (function() {
16
- __extends(List, Spine.Controller);
10
+
11
+ Spine.List = (function(_super) {
12
+
13
+ __extends(List, _super);
14
+
17
15
  List.prototype.events = {
18
16
  'click .item': 'click'
19
17
  };
18
+
20
19
  List.prototype.selectFirst = false;
20
+
21
21
  function List() {
22
22
  this.change = __bind(this.change, this); List.__super__.constructor.apply(this, arguments);
23
23
  this.bind('change', this.change);
24
24
  }
25
+
25
26
  List.prototype.template = function() {
26
27
  return arguments[0];
27
28
  };
29
+
28
30
  List.prototype.change = function(item) {
29
- if (!item) {
31
+ this.current = item;
32
+ if (!this.current) {
33
+ this.children().removeClass('active');
30
34
  return;
31
35
  }
32
- this.current = item;
33
36
  this.children().removeClass('active');
34
37
  return this.children().forItem(this.current).addClass('active');
35
38
  };
39
+
36
40
  List.prototype.render = function(items) {
37
- if (items) {
38
- this.items = items;
39
- }
41
+ if (items) this.items = items;
40
42
  this.html(this.template(this.items));
41
43
  this.change(this.current);
42
44
  if (this.selectFirst) {
@@ -45,18 +47,24 @@
45
47
  }
46
48
  }
47
49
  };
50
+
48
51
  List.prototype.children = function(sel) {
49
52
  return this.el.children(sel);
50
53
  };
54
+
51
55
  List.prototype.click = function(e) {
52
56
  var item;
53
57
  item = $(e.currentTarget).item();
54
58
  this.trigger('change', item);
55
- return false;
59
+ return true;
56
60
  };
61
+
57
62
  return List;
58
- })();
63
+
64
+ })(Spine.Controller);
65
+
59
66
  if (typeof module !== "undefined" && module !== null) {
60
67
  module.exports = Spine.List;
61
68
  }
69
+
62
70
  }).call(this);