spine-rails 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,290 +0,0 @@
1
- (function() {
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
-
10
- $ = Spine.$;
11
-
12
- Model = Spine.Model;
13
-
14
- Ajax = {
15
- getURL: function(object) {
16
- return object && (typeof object.url === "function" ? object.url() : void 0) || object.url;
17
- },
18
- enabled: true,
19
- pending: false,
20
- requests: [],
21
- disable: function(callback) {
22
- if (this.enabled) {
23
- this.enabled = false;
24
- callback();
25
- return this.enabled = true;
26
- } else {
27
- return callback();
28
- }
29
- },
30
- requestNext: function() {
31
- var next;
32
- next = this.requests.shift();
33
- if (next) {
34
- return this.request(next);
35
- } else {
36
- return this.pending = false;
37
- }
38
- },
39
- request: function(callback) {
40
- var _this = this;
41
- return (callback()).complete(function() {
42
- return _this.requestNext();
43
- });
44
- },
45
- queue: function(callback) {
46
- if (!this.enabled) return;
47
- if (this.pending) {
48
- this.requests.push(callback);
49
- } else {
50
- this.pending = true;
51
- this.request(callback);
52
- }
53
- return callback;
54
- }
55
- };
56
-
57
- Base = (function() {
58
-
59
- function Base() {}
60
-
61
- Base.prototype.defaults = {
62
- contentType: 'application/json',
63
- dataType: 'json',
64
- processData: false,
65
- headers: {
66
- 'X-Requested-With': 'XMLHttpRequest'
67
- }
68
- };
69
-
70
- Base.prototype.ajax = function(params, defaults) {
71
- return $.ajax($.extend({}, this.defaults, defaults, params));
72
- };
73
-
74
- Base.prototype.queue = function(callback) {
75
- return Ajax.queue(callback);
76
- };
77
-
78
- return Base;
79
-
80
- })();
81
-
82
- Collection = (function(_super) {
83
-
84
- __extends(Collection, _super);
85
-
86
- function Collection(model) {
87
- this.model = model;
88
- this.errorResponse = __bind(this.errorResponse, this);
89
- this.recordsResponse = __bind(this.recordsResponse, this);
90
- }
91
-
92
- Collection.prototype.find = function(id, params) {
93
- var record;
94
- record = new this.model({
95
- id: id
96
- });
97
- return this.ajax(params, {
98
- type: 'GET',
99
- url: Ajax.getURL(record)
100
- }).success(this.recordsResponse).error(this.errorResponse);
101
- };
102
-
103
- Collection.prototype.all = function(params) {
104
- return this.ajax(params, {
105
- type: 'GET',
106
- url: Ajax.getURL(this.model)
107
- }).success(this.recordsResponse).error(this.errorResponse);
108
- };
109
-
110
- Collection.prototype.fetch = function(params, options) {
111
- var id,
112
- _this = this;
113
- if (params == null) params = {};
114
- if (options == null) options = {};
115
- if (id = params.id) {
116
- delete params.id;
117
- return this.find(id, params).success(function(record) {
118
- return _this.model.refresh(record, options);
119
- });
120
- } else {
121
- return this.all(params).success(function(records) {
122
- return _this.model.refresh(records, options);
123
- });
124
- }
125
- };
126
-
127
- Collection.prototype.recordsResponse = function(data, status, xhr) {
128
- return this.model.trigger('ajaxSuccess', null, status, xhr);
129
- };
130
-
131
- Collection.prototype.errorResponse = function(xhr, statusText, error) {
132
- return this.model.trigger('ajaxError', null, xhr, statusText, error);
133
- };
134
-
135
- return Collection;
136
-
137
- })(Base);
138
-
139
- Singleton = (function(_super) {
140
-
141
- __extends(Singleton, _super);
142
-
143
- function Singleton(record) {
144
- this.record = record;
145
- this.errorResponse = __bind(this.errorResponse, this);
146
- this.recordResponse = __bind(this.recordResponse, this);
147
- this.model = this.record.constructor;
148
- }
149
-
150
- Singleton.prototype.reload = function(params, options) {
151
- var _this = this;
152
- return this.queue(function() {
153
- return _this.ajax(params, {
154
- type: 'GET',
155
- url: Ajax.getURL(_this.record)
156
- }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
157
- });
158
- };
159
-
160
- Singleton.prototype.create = function(params, options) {
161
- var _this = this;
162
- return this.queue(function() {
163
- return _this.ajax(params, {
164
- type: 'POST',
165
- data: JSON.stringify(_this.record),
166
- url: Ajax.getURL(_this.model)
167
- }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
168
- });
169
- };
170
-
171
- Singleton.prototype.update = function(params, options) {
172
- var _this = this;
173
- return this.queue(function() {
174
- return _this.ajax(params, {
175
- type: 'PUT',
176
- data: JSON.stringify(_this.record),
177
- url: Ajax.getURL(_this.record)
178
- }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
179
- });
180
- };
181
-
182
- Singleton.prototype.destroy = function(params, options) {
183
- var _this = this;
184
- return this.queue(function() {
185
- return _this.ajax(params, {
186
- type: 'DELETE',
187
- url: Ajax.getURL(_this.record)
188
- }).success(_this.recordResponse(options)).error(_this.errorResponse(options));
189
- });
190
- };
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);
201
- }
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
- };
213
- };
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
- };
223
- };
224
-
225
- return Singleton;
226
-
227
- })(Base);
228
-
229
- Model.host = '';
230
-
231
- Include = {
232
- ajax: function() {
233
- return new Singleton(this);
234
- },
235
- url: function() {
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('/');
243
- }
244
- };
245
-
246
- Extend = {
247
- ajax: function() {
248
- return new Collection(this);
249
- },
250
- url: function() {
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('/');
256
- }
257
- };
258
-
259
- Model.Ajax = {
260
- extended: function() {
261
- this.fetch(this.ajaxFetch);
262
- this.change(this.ajaxChange);
263
- this.extend(Extend);
264
- return this.include(Include);
265
- },
266
- ajaxFetch: function() {
267
- var _ref;
268
- return (_ref = this.ajax()).fetch.apply(_ref, arguments);
269
- },
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);
274
- }
275
- };
276
-
277
- Model.Ajax.Methods = {
278
- extended: function() {
279
- this.extend(Extend);
280
- return this.include(Include);
281
- }
282
- };
283
-
284
- Ajax.defaults = Base.prototype.defaults;
285
-
286
- Spine.Ajax = Ajax;
287
-
288
- if (typeof module !== "undefined" && module !== null) module.exports = Ajax;
289
-
290
- }).call(this);
@@ -1,70 +0,0 @@
1
- (function() {
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
-
9
- $ = Spine.$;
10
-
11
- Spine.List = (function(_super) {
12
-
13
- __extends(List, _super);
14
-
15
- List.prototype.events = {
16
- 'click .item': 'click'
17
- };
18
-
19
- List.prototype.selectFirst = false;
20
-
21
- function List() {
22
- this.change = __bind(this.change, this); List.__super__.constructor.apply(this, arguments);
23
- this.bind('change', this.change);
24
- }
25
-
26
- List.prototype.template = function() {
27
- return arguments[0];
28
- };
29
-
30
- List.prototype.change = function(item) {
31
- this.current = item;
32
- if (!this.current) {
33
- this.children().removeClass('active');
34
- return;
35
- }
36
- this.children().removeClass('active');
37
- return this.children().forItem(this.current).addClass('active');
38
- };
39
-
40
- List.prototype.render = function(items) {
41
- if (items) this.items = items;
42
- this.html(this.template(this.items));
43
- this.change(this.current);
44
- if (this.selectFirst) {
45
- if (!this.children('.active').length) {
46
- return this.children(':first').click();
47
- }
48
- }
49
- };
50
-
51
- List.prototype.children = function(sel) {
52
- return this.el.children(sel);
53
- };
54
-
55
- List.prototype.click = function(e) {
56
- var item;
57
- item = $(e.currentTarget).item();
58
- this.trigger('change', item);
59
- return true;
60
- };
61
-
62
- return List;
63
-
64
- })(Spine.Controller);
65
-
66
- if (typeof module !== "undefined" && module !== null) {
67
- module.exports = Spine.List;
68
- }
69
-
70
- }).call(this);
@@ -1,28 +0,0 @@
1
- (function() {
2
-
3
- if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
4
-
5
- Spine.Model.Local = {
6
- extended: function() {
7
- this.change(this.saveLocal);
8
- return this.fetch(this.loadLocal);
9
- },
10
- saveLocal: function() {
11
- var result;
12
- result = JSON.stringify(this);
13
- return localStorage[this.className] = result;
14
- },
15
- loadLocal: function() {
16
- var result;
17
- result = localStorage[this.className];
18
- return this.refresh(result || [], {
19
- clear: true
20
- });
21
- }
22
- };
23
-
24
- if (typeof module !== "undefined" && module !== null) {
25
- module.exports = Spine.Model.Local;
26
- }
27
-
28
- }).call(this);
@@ -1,154 +0,0 @@
1
- (function() {
2
- var $,
3
- __hasProp = Object.prototype.hasOwnProperty,
4
- __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; },
5
- __slice = Array.prototype.slice;
6
-
7
- if (typeof Spine === "undefined" || Spine === null) Spine = require('spine');
8
-
9
- $ = Spine.$;
10
-
11
- Spine.Manager = (function(_super) {
12
-
13
- __extends(Manager, _super);
14
-
15
- Manager.include(Spine.Events);
16
-
17
- function Manager() {
18
- this.controllers = [];
19
- this.bind('change', this.change);
20
- this.add.apply(this, arguments);
21
- }
22
-
23
- Manager.prototype.add = function() {
24
- var cont, controllers, _i, _len, _results;
25
- controllers = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
26
- _results = [];
27
- for (_i = 0, _len = controllers.length; _i < _len; _i++) {
28
- cont = controllers[_i];
29
- _results.push(this.addOne(cont));
30
- }
31
- return _results;
32
- };
33
-
34
- Manager.prototype.addOne = function(controller) {
35
- var _this = this;
36
- controller.bind('active', function() {
37
- var args;
38
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
39
- return _this.trigger.apply(_this, ['change', controller].concat(__slice.call(args)));
40
- });
41
- controller.bind('release', function() {
42
- return _this.controllers.splice(_this.controllers.indexOf(controller), 1);
43
- });
44
- return this.controllers.push(controller);
45
- };
46
-
47
- Manager.prototype.deactivate = function() {
48
- return this.trigger.apply(this, ['change', false].concat(__slice.call(arguments)));
49
- };
50
-
51
- Manager.prototype.change = function() {
52
- var args, cont, current, _i, _len, _ref, _results;
53
- current = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
54
- _ref = this.controllers;
55
- _results = [];
56
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
57
- cont = _ref[_i];
58
- if (cont === current) {
59
- _results.push(cont.activate.apply(cont, args));
60
- } else {
61
- _results.push(cont.deactivate.apply(cont, args));
62
- }
63
- }
64
- return _results;
65
- };
66
-
67
- return Manager;
68
-
69
- })(Spine.Module);
70
-
71
- Spine.Controller.include({
72
- active: function() {
73
- var args;
74
- args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
75
- if (typeof args[0] === 'function') {
76
- this.bind('active', args[0]);
77
- } else {
78
- args.unshift('active');
79
- this.trigger.apply(this, args);
80
- }
81
- return this;
82
- },
83
- isActive: function() {
84
- return this.el.hasClass('active');
85
- },
86
- activate: function() {
87
- this.el.addClass('active');
88
- return this;
89
- },
90
- deactivate: function() {
91
- this.el.removeClass('active');
92
- return this;
93
- }
94
- });
95
-
96
- Spine.Stack = (function(_super) {
97
-
98
- __extends(Stack, _super);
99
-
100
- Stack.prototype.controllers = {};
101
-
102
- Stack.prototype.routes = {};
103
-
104
- Stack.prototype.className = 'spine stack';
105
-
106
- function Stack() {
107
- var key, value, _fn, _ref, _ref2,
108
- _this = this;
109
- Stack.__super__.constructor.apply(this, arguments);
110
- this.manager = new Spine.Manager;
111
- this.manager.bind('change', function() {
112
- var args, controller;
113
- controller = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
114
- if (controller) return _this.active.apply(_this, args);
115
- });
116
- _ref = this.controllers;
117
- for (key in _ref) {
118
- value = _ref[key];
119
- this[key] = new value({
120
- stack: this
121
- });
122
- this.add(this[key]);
123
- }
124
- _ref2 = this.routes;
125
- _fn = function(key, value) {
126
- var callback;
127
- if (typeof value === 'function') callback = value;
128
- callback || (callback = function() {
129
- var _ref3;
130
- return (_ref3 = _this[value]).active.apply(_ref3, arguments);
131
- });
132
- return _this.route(key, callback);
133
- };
134
- for (key in _ref2) {
135
- value = _ref2[key];
136
- _fn(key, value);
137
- }
138
- if (this["default"]) this[this["default"]].active();
139
- }
140
-
141
- Stack.prototype.add = function(controller) {
142
- this.manager.add(controller);
143
- return this.append(controller);
144
- };
145
-
146
- return Stack;
147
-
148
- })(Spine.Controller);
149
-
150
- if (typeof module !== "undefined" && module !== null) {
151
- module.exports = Spine.Manager;
152
- }
153
-
154
- }).call(this);