canjs-rails 0.1.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,43 @@
1
- (function(can, window, undefined){
1
+ /*
2
+ * CanJS - 1.1.2 (2012-11-28)
3
+ * http://canjs.us/
4
+ * Copyright (c) 2012 Bitovi
5
+ * Licensed MIT
6
+ */
7
+ (function (can, window, undefined) {
8
+ // ## can/control/view/view.js
2
9
  var URI = steal.URI || steal.File;
3
-
4
- can.Control.getFolder = function() {
10
+
11
+ can.Control.getFolder = function () {
5
12
  return can.underscore(this.fullName.replace(/\./g, "/")).replace("/Controllers", "");
6
13
  };
7
14
 
8
- can.Control._calculatePosition = function( Class, view ) {
15
+ can.Control._calculatePosition = function (Class, view) {
9
16
  var classParts = Class.fullName.split('.'),
10
17
  classPartsWithoutPrefix = classParts.slice(0);
11
- classPartsWithoutPrefix.splice(0, 2),
12
- action_name = "init"; // Remove prefix (usually 2 elements)
13
-
18
+ classPartsWithoutPrefix.splice(0, 2), action_name = "init"; // Remove prefix (usually 2 elements)
14
19
  var hasControllers = (classParts.length > 2) && classParts[1] == 'Controllers',
15
- path = hasControllers? can.underscore(classParts[0]): can.underscore(classParts.join("/")),
20
+ path = hasControllers ? can.underscore(classParts[0]) : can.underscore(classParts.join("/")),
16
21
  controller_name = can.underscore(classPartsWithoutPrefix.join('/')).toLowerCase(),
17
22
  suffix = (typeof view == "string" && /\.[\w\d]+$/.test(view)) ? "" : can.view.ext;
18
-
23
+
19
24
  //calculate view
20
- if ( typeof view == "string" ) {
21
- if ( view.substr(0, 2) == "//" ) { //leave where it is
25
+ if (typeof view == "string") {
26
+ if (view.substr(0, 2) == "//") { //leave where it is
22
27
  } else {
23
- view = "//" + URI(path).join( 'views/' + (view.indexOf('/') !== -1 ? view : (hasControllers ? controller_name + '/' : "") + view)) + suffix;
28
+ view = "//" + URI(path).join('views/' + (view.indexOf('/') !== -1 ? view : (hasControllers ? controller_name + '/' : "") + view)) + suffix;
24
29
  }
25
- } else if (!view ) {
26
- view = "//" + URI(path).join('views/' + (hasControllers ? controller_name + '/' : "") + action_name.replace(/\.|#/g, '').replace(/ /g, '_'))+ suffix;
30
+ } else if (!view) {
31
+ view = "//" + URI(path).join('views/' + (hasControllers ? controller_name + '/' : "") + action_name.replace(/\.|#/g, '').replace(/ /g, '_')) + suffix;
27
32
  }
28
33
  return view;
29
34
  };
30
-
31
- var calculateHelpers = function( myhelpers ) {
35
+
36
+ var calculateHelpers = function (myhelpers) {
32
37
  var helpers = {};
33
- if ( myhelpers ) {
34
- if ( can.isArray(myhelpers) ) {
35
- for ( var h = 0; h < myhelpers.length; h++ ) {
38
+ if (myhelpers) {
39
+ if (can.isArray(myhelpers)) {
40
+ for (var h = 0; h < myhelpers.length; h++) {
36
41
  can.extend(helpers, myhelpers[h]);
37
42
  }
38
43
  }
@@ -40,39 +45,39 @@
40
45
  can.extend(helpers, myhelpers);
41
46
  }
42
47
  } else {
43
- if ( this._default_helpers ) {
48
+ if (this._default_helpers) {
44
49
  helpers = this._default_helpers;
45
50
  }
46
-
51
+
47
52
  //load from name
48
53
  var current = window;
49
54
  var parts = this.constructor.fullName.split(/\./);
50
- for ( var i = 0; i < parts.length; i++ ) {
51
- if(current){
52
- if ( typeof current.Helpers == 'object' ) {
55
+ for (var i = 0; i < parts.length; i++) {
56
+ if (current) {
57
+ if (typeof current.Helpers == 'object') {
53
58
  can.extend(helpers, current.Helpers);
54
59
  }
55
60
  current = current[parts[i]];
56
61
  }
57
62
  }
58
-
59
- if (current && typeof current.Helpers == 'object' ) {
63
+
64
+ if (current && typeof current.Helpers == 'object') {
60
65
  can.extend(helpers, current.Helpers);
61
66
  }
62
-
67
+
63
68
  this._default_helpers = helpers;
64
69
  }
65
70
  return helpers;
66
71
  };
67
72
 
68
- can.Control.prototype.view = function( view, data, myhelpers ) {
73
+ can.Control.prototype.view = function (view, data, myhelpers) {
69
74
  //shift args if no view is provided
70
- if ( typeof view != "string" && !myhelpers ) {
75
+ if (typeof view != "string" && !myhelpers) {
71
76
  myhelpers = data;
72
77
  data = view;
73
78
  view = null;
74
79
  }
75
-
80
+
76
81
  //guess from controller name
77
82
  view = can.Control._calculatePosition(this.constructor, view, this.called);
78
83
 
@@ -85,4 +90,5 @@
85
90
  return can.view(view, data, helpers); //what about controllers in other folders?
86
91
  };
87
92
 
88
- })(this.can, this )
93
+
94
+ })(can, this);
@@ -1,326 +1,253 @@
1
- (function(can, window, undefined){
2
-
3
- var isArray = can.isArray,
4
- // essentially returns an object that has all the must have comparisons ...
5
- // must haves, do not return true when provided undefined
6
- cleanSet = function(obj, compares){
7
- var copy = can.extend({}, obj);
8
- for(var prop in copy) {
9
- var compare = compares[prop] === undefined ? compares["*"] : compares[prop];
10
- if( same(copy[prop], undefined, compare ) ) {
11
- delete copy[prop]
1
+ /*
2
+ * CanJS - 1.1.2 (2012-11-28)
3
+ * http://canjs.us/
4
+ * Copyright (c) 2012 Bitovi
5
+ * Licensed MIT
6
+ */
7
+ (function (can, window, undefined) {
8
+ // ## can/util/object/object.js
9
+
10
+ var isArray = can.isArray,
11
+ // essentially returns an object that has all the must have comparisons ...
12
+ // must haves, do not return true when provided undefined
13
+ cleanSet = function (obj, compares) {
14
+ var copy = can.extend({}, obj);
15
+ for (var prop in copy) {
16
+ var compare = compares[prop] === undefined ? compares["*"] : compares[prop];
17
+ if (same(copy[prop], undefined, compare)) {
18
+ delete copy[prop]
19
+ }
12
20
  }
21
+ return copy;
22
+ },
23
+ propCount = function (obj) {
24
+ var count = 0;
25
+ for (var prop in obj) count++;
26
+ return count;
27
+ };
28
+
29
+ can.Object = {};
30
+
31
+ var same = can.Object.same = function (a, b, compares, aParent, bParent, deep) {
32
+ var aType = typeof a,
33
+ aArray = isArray(a),
34
+ comparesType = typeof compares,
35
+ compare;
36
+
37
+ if (comparesType == 'string' || compares === null) {
38
+ compares = compareMethods[compares];
39
+ comparesType = 'function'
13
40
  }
14
- return copy;
15
- },
16
- propCount = function(obj){
17
- var count = 0;
18
- for(var prop in obj) count++;
19
- return count;
20
- };
41
+ if (comparesType == 'function') {
42
+ return compares(a, b, aParent, bParent)
43
+ }
44
+ compares = compares || {};
21
45
 
22
- /**
23
- * @class can.Object
24
- * @parent can.util
25
- *
26
- * Object contains several helper methods that
27
- * help compare objects.
28
- *
29
- * ## same
30
- *
31
- * Returns true if two objects are similar.
32
- *
33
- * can.Object.same({foo: "bar"} , {bar: "foo"}) //-> false
34
- *
35
- * ## subset
36
- *
37
- * Returns true if an object is a set of another set.
38
- *
39
- * can.Object.subset({}, {foo: "bar"} ) //-> true
40
- *
41
- * ## subsets
42
- *
43
- * Returns the subsets of an object
44
- *
45
- * can.Object.subsets({userId: 20},
46
- * [
47
- * {userId: 20, limit: 30},
48
- * {userId: 5},
49
- * {}
50
- * ])
51
- * //-> [{userId: 20, limit: 30}]
52
- */
53
- can.Object = {};
54
-
55
- /**
56
- * @function same
57
- * Returns if two objects are the same. It takes an optional compares object that
58
- * can be used to make comparisons.
59
- *
60
- * This function does not work with objects that create circular references.
61
- *
62
- * ## Examples
63
- *
64
- * can.Object.same({name: "Justin"},
65
- * {name: "JUSTIN"}) //-> false
66
- *
67
- * // ignore the name property
68
- * can.Object.same({name: "Brian"},
69
- * {name: "JUSTIN"},
70
- * {name: null}) //-> true
71
- *
72
- * // ignore case
73
- * can.Object.same({name: "Justin"},
74
- * {name: "JUSTIN"},
75
- * {name: "i"}) //-> true
76
- *
77
- * // deep rule
78
- * can.Object.same({ person : { name: "Justin" } },
79
- * { person : { name: "JUSTIN" } },
80
- * { person : { name: "i" } }) //-> true
81
- *
82
- * // supplied compare function
83
- * can.Object.same({age: "Thirty"},
84
- * {age: 30},
85
- * {age: function( a, b ){
86
- * if( a == "Thirty" ) {
87
- * a = 30
88
- * }
89
- * if( b == "Thirty" ) {
90
- * b = 30
91
- * }
92
- * return a === b;
93
- * }}) //-> true
94
- *
95
- * @param {Object} a an object to compare
96
- * @param {Object} b an object to compare
97
- * @param {Object} [compares] an object that indicates how to
98
- * compare specific properties.
99
- * Typically this is a name / value pair
100
- *
101
- * can.Object.same({name: "Justin"},{name: "JUSTIN"},{name: "i"})
102
- *
103
- * There are two compare functions that you can specify with a string:
104
- *
105
- * - 'i' - ignores case
106
- * - null - ignores this property
107
- *
108
- * @param {Object} [deep] used internally
109
- */
110
- var same = can.Object.same = function(a, b, compares, aParent, bParent, deep){
111
- var aType = typeof a,
112
- aArray = isArray(a),
113
- comparesType = typeof compares,
114
- compare;
115
-
116
- if(comparesType == 'string' || compares === null ){
117
- compares = compareMethods[compares];
118
- comparesType = 'function'
119
- }
120
- if(comparesType == 'function'){
121
- return compares(a, b, aParent, bParent)
122
- }
123
- compares = compares || {};
124
-
125
- if(a instanceof Date){
126
- return a === b;
127
- }
128
- if(deep === -1){
129
- return aType === 'object' || a === b;
130
- }
131
- if(aType !== typeof b || aArray !== isArray(b)){
132
- return false;
133
- }
134
- if(a === b){
135
- return true;
136
- }
137
- if(aArray){
138
- if(a.length !== b.length){
46
+ if (a instanceof Date) {
47
+ return a === b;
48
+ }
49
+ if (deep === -1) {
50
+ return aType === 'object' || a === b;
51
+ }
52
+ if (aType !== typeof b || aArray !== isArray(b)) {
139
53
  return false;
140
54
  }
141
- for(var i =0; i < a.length; i ++){
142
- compare = compares[i] === undefined ? compares["*"] : compares[i]
143
- if(!same(a[i],b[i], a, b, compare )){
55
+ if (a === b) {
56
+ return true;
57
+ }
58
+ if (aArray) {
59
+ if (a.length !== b.length) {
144
60
  return false;
145
61
  }
146
- };
147
- return true;
148
- } else if(aType === "object" || aType === 'function'){
149
- var bCopy = can.extend({}, b);
150
- for(var prop in a){
151
- compare = compares[prop] === undefined ? compares["*"] : compares[prop];
152
- if(! same( a[prop], b[prop], compare , a, b, deep === false ? -1 : undefined )){
153
- return false;
62
+ for (var i = 0; i < a.length; i++) {
63
+ compare = compares[i] === undefined ? compares["*"] : compares[i]
64
+ if (!same(a[i], b[i], a, b, compare)) {
65
+ return false;
66
+ }
67
+ };
68
+ return true;
69
+ } else if (aType === "object" || aType === 'function') {
70
+ var bCopy = can.extend({}, b);
71
+ for (var prop in a) {
72
+ compare = compares[prop] === undefined ? compares["*"] : compares[prop];
73
+ if (!same(a[prop], b[prop], compare, a, b, deep === false ? -1 : undefined)) {
74
+ return false;
75
+ }
76
+ delete bCopy[prop];
77
+ }
78
+ // go through bCopy props ... if there is no compare .. return false
79
+ for (prop in bCopy) {
80
+ if (compares[prop] === undefined || !same(undefined, b[prop], compares[prop], a, b, deep === false ? -1 : undefined)) {
81
+ return false;
82
+ }
154
83
  }
155
- delete bCopy[prop];
84
+ return true;
156
85
  }
157
- // go through bCopy props ... if there is no compare .. return false
158
- for(prop in bCopy){
159
- if( compares[prop] === undefined ||
160
- ! same( undefined, b[prop], compares[prop] , a, b, deep === false ? -1 : undefined )){
86
+ return false;
87
+ };
88
+
89
+ can.Object.subsets = function (checkSet, sets, compares) {
90
+ var len = sets.length,
91
+ subsets = [],
92
+ checkPropCount = propCount(checkSet),
93
+ setLength;
94
+
95
+ for (var i = 0; i < len; i++) {
96
+ //check this subset
97
+ var set = sets[i];
98
+ if (can.Object.subset(checkSet, set, compares)) {
99
+ subsets.push(set)
100
+ }
101
+ }
102
+ return subsets;
103
+ };
104
+
105
+ can.Object.subset = function (subset, set, compares) {
106
+ // go through set {type: 'folder'} and make sure every property
107
+ // is in subset {type: 'folder', parentId :5}
108
+ // then make sure that set has fewer properties
109
+ // make sure we are only checking 'important' properties
110
+ // in subset (ones that have to have a value)
111
+ var setPropCount = 0,
112
+ compares = compares || {};
113
+
114
+ for (var prop in set) {
115
+
116
+ if (!same(subset[prop], set[prop], compares[prop], subset, set)) {
161
117
  return false;
162
118
  }
163
119
  }
164
120
  return true;
165
- }
166
- return false;
167
- };
168
-
169
- /**
170
- * @function subsets
171
- * Returns the sets in 'sets' that are a subset of checkSet
172
- * @param {Object} checkSet
173
- * @param {Object} sets
174
- */
175
- can.Object.subsets = function(checkSet, sets, compares){
176
- var len = sets.length,
177
- subsets = [],
178
- checkPropCount = propCount(checkSet),
179
- setLength;
180
-
181
- for(var i =0; i < len; i++){
182
- //check this subset
183
- var set = sets[i];
184
- if( can.Object.subset(checkSet, set, compares) ){
185
- subsets.push(set)
186
- }
187
121
  }
188
- return subsets;
189
- };
190
- /**
191
- * @function subset
192
- * Compares if checkSet is a subset of set
193
- * @param {Object} checkSet
194
- * @param {Object} set
195
- * @param {Object} [compares]
196
- * @param {Object} [checkPropCount]
197
- */
198
- can.Object.subset = function(subset, set, compares){
199
- // go through set {type: 'folder'} and make sure every property
200
- // is in subset {type: 'folder', parentId :5}
201
- // then make sure that set has fewer properties
202
- // make sure we are only checking 'important' properties
203
- // in subset (ones that have to have a value)
204
-
205
- var setPropCount =0,
206
- compares = compares || {};
207
-
208
- for(var prop in set){
209
122
 
210
- if(! same(subset[prop], set[prop], compares[prop], subset, set ) ){
211
- return false;
212
- }
123
+ var compareMethods = {
124
+ "null": function () {
125
+ return true;
126
+ },
127
+ i: function (a, b) {
128
+ return ("" + a).toLowerCase() == ("" + b).toLowerCase()
129
+ }
213
130
  }
214
- return true;
215
- }
216
131
 
217
-
218
- var compareMethods = {
219
- "null" : function(){
220
- return true;
221
- },
222
- i : function(a, b){
223
- return (""+a).toLowerCase() == (""+b).toLowerCase()
132
+ // ## can/util/fixture/fixture.js
133
+ // Get the URL from old Steal root, new Steal config or can.fixture.rootUrl
134
+ var getUrl = function (url) {
135
+ if (typeof steal !== 'undefined') {
136
+ if (can.isFunction(steal.config)) {
137
+ return steal.config().root.mapJoin(url).toString();
138
+ }
139
+ return steal.root.join(url).toString();
140
+ }
141
+ return (can.fixture.rootUrl || '') + url;
224
142
  }
225
- }
226
-
227
-
228
- ;
229
-
230
143
 
231
144
  var updateSettings = function (settings, originalOptions) {
232
- if (!can.fixture.on) {
233
- return;
234
- }
145
+ if (!can.fixture.on) {
146
+ return;
147
+ }
235
148
 
236
- //simple wrapper for logging
237
- var log = function () {
149
+ //simple wrapper for logging
150
+ var _logger = function (type, arr) {
151
+ if (console.log.apply) {
152
+ Function.prototype.call.apply(console[type], [console].concat(arr));
153
+ // console[type].apply(console, arr)
154
+ } else {
155
+ console[type](arr)
156
+ }
157
+ },
158
+ log = function () {
238
159
  if (window.console && console.log) {
239
- console.log.apply(console, Array.prototype.slice.call(arguments));
160
+ Array.prototype.unshift.call(arguments, 'fixture INFO:');
161
+ _logger("log", Array.prototype.slice.call(arguments));
162
+ }
163
+ else if (window.opera && window.opera.postError) {
164
+ opera.postError("fixture INFO: " + Array.prototype.join.call(arguments, ','));
240
165
  }
241
166
  }
242
167
 
243
168
  // We always need the type which can also be called method, default to GET
244
169
  settings.type = settings.type || settings.method || 'GET';
245
170
 
246
- // add the fixture option if programmed in
247
- var data = overwrite(settings);
171
+ // add the fixture option if programmed in
172
+ var data = overwrite(settings);
248
173
 
249
- // if we don't have a fixture, do nothing
250
- if (!settings.fixture) {
251
- if (window.location.protocol === "file:") {
252
- log("ajax request to " + settings.url + ", no fixture found");
253
- }
254
- return;
174
+ // if we don't have a fixture, do nothing
175
+ if (!settings.fixture) {
176
+ if (window.location.protocol === "file:") {
177
+ log("ajax request to " + settings.url + ", no fixture found");
255
178
  }
179
+ return;
180
+ }
181
+
182
+ //if referencing something else, update the fixture option
183
+ if (typeof settings.fixture === "string" && can.fixture[settings.fixture]) {
184
+ settings.fixture = can.fixture[settings.fixture];
185
+ }
256
186
 
257
- //if referencing something else, update the fixture option
258
- if (typeof settings.fixture === "string" && can.fixture[settings.fixture]) {
259
- settings.fixture = can.fixture[settings.fixture];
187
+ // if a string, we just point to the right url
188
+ if (typeof settings.fixture == "string") {
189
+ var url = settings.fixture;
190
+
191
+ if (/^\/\//.test(url)) {
192
+ // this lets us use rootUrl w/o having steal...
193
+ url = getUrl(settings.fixture.substr(2));
260
194
  }
261
195
 
262
- // if a string, we just point to the right url
263
- if (typeof settings.fixture == "string") {
264
- var url = settings.fixture;
196
+ if (data) {
197
+ // Template static fixture URLs
198
+ url = can.sub(url, data);
199
+ }
265
200
 
266
- if (/^\/\//.test(url)) {
267
- // this lets us use rootUrl w/o having steal...
268
- url = can.fixture.rootUrl === steal.root ?
269
- steal.root.mapJoin(settings.fixture.substr(2)) + '' :
270
- can.fixture.rootUrl + settings.fixture.substr(2);
271
- }
201
+ delete settings.fixture;
272
202
 
273
- delete settings.fixture;
274
203
 
275
- //@steal-remove-start
276
- log("looking for fixture in " + url);
277
- //@steal-remove-end
278
204
 
279
- settings.url = url;
280
- settings.data = null;
281
- settings.type = "GET";
282
- if (!settings.error) {
283
- settings.error = function (xhr, error, message) {
284
- throw "fixtures.js Error " + error + " " + message;
285
- };
286
- }
205
+ settings.url = url;
206
+ settings.data = null;
207
+ settings.type = "GET";
208
+ if (!settings.error) {
209
+ settings.error = function (xhr, error, message) {
210
+ throw "fixtures.js Error " + error + " " + message;
211
+ };
287
212
  }
288
- else {
289
- //@steal-remove-start
290
- log("using a dynamic fixture for " + settings.type + " " + settings.url);
291
- //@steal-remove-end
213
+ }
214
+ else {
292
215
 
293
- //it's a function ... add the fixture datatype so our fixture transport handles it
294
- // TODO: make everything go here for timing and other fun stuff
295
- // add to settings data from fixture ...
296
- settings.dataTypes && settings.dataTypes.splice(0, 0, "fixture");
297
216
 
298
- if (data && originalOptions) {
299
- can.extend(originalOptions.data, data)
300
- }
217
+ //it's a function ... add the fixture datatype so our fixture transport handles it
218
+ // TODO: make everything go here for timing and other fun stuff
219
+ // add to settings data from fixture ...
220
+ settings.dataTypes && settings.dataTypes.splice(0, 0, "fixture");
221
+
222
+ if (data && originalOptions) {
223
+ can.extend(originalOptions.data, data)
301
224
  }
302
- },
225
+ }
226
+ },
303
227
  // A helper function that takes what's called with response
304
228
  // and moves some common args around to make it easier to call
305
- extractResponse = function(status, statusText, responses, headers) {
229
+ extractResponse = function (status, statusText, responses, headers) {
306
230
  // if we get response(RESPONSES, HEADERS)
307
- if(typeof status != "number"){
231
+ if (typeof status != "number") {
308
232
  headers = statusText;
309
233
  responses = status;
310
234
  statusText = "success"
311
235
  status = 200;
312
236
  }
313
237
  // if we get response(200, RESPONSES, HEADERS)
314
- if(typeof statusText != "string"){
238
+ if (typeof statusText != "string") {
315
239
  headers = responses;
316
240
  responses = statusText;
317
241
  statusText = "success";
318
242
  }
243
+ if (status >= 400 && status <= 599) {
244
+ this.dataType = "text"
245
+ }
319
246
  return [status, statusText, extractResponses(this, responses), headers];
320
247
  },
321
248
  // If we get data instead of responses,
322
249
  // make sure we provide a response type that matches the first datatype (typically json)
323
- extractResponses = function(settings, responses){
250
+ extractResponses = function (settings, responses) {
324
251
  var next = settings.dataTypes ? settings.dataTypes[0] : (settings.dataType || 'json');
325
252
  if (!responses || !responses[next]) {
326
253
  var tmp = {}
@@ -349,14 +276,14 @@ var compareMethods = {
349
276
  // we'll immediately wait the delay time for all fixtures
350
277
  timeout = setTimeout(function () {
351
278
  // if the user wants to call success on their own, we allow it ...
352
- var success = function() {
353
- if(stopped === false) {
354
- callback.apply(null, extractResponse.apply(s, arguments) );
279
+ var success = function () {
280
+ if (stopped === false) {
281
+ callback.apply(null, extractResponse.apply(s, arguments));
355
282
  }
356
283
  },
357
- // get the result form the fixture
358
- result = s.fixture(original, success, headers, s);
359
- if(result !== undefined) {
284
+ // get the result form the fixture
285
+ result = s.fixture(original, success, headers, s);
286
+ if (result !== undefined) {
360
287
  // make sure the result has the right dataType
361
288
  callback(200, "success", extractResponses(s, result), {});
362
289
  }
@@ -377,8 +304,7 @@ var compareMethods = {
377
304
  stopped = false;
378
305
 
379
306
  //TODO this should work with response
380
- d.getResponseHeader = function () {
381
- }
307
+ d.getResponseHeader = function () {}
382
308
 
383
309
  // call success and fail
384
310
  d.then(settings.success, settings.fail);
@@ -392,24 +318,24 @@ var compareMethods = {
392
318
  // set a timeout that simulates making a request ....
393
319
  timeout = setTimeout(function () {
394
320
  // if the user wants to call success on their own, we allow it ...
395
- var success = function() {
321
+ var success = function () {
396
322
  var response = extractResponse.apply(settings, arguments),
397
323
  status = response[0];
398
324
 
399
- if ( (status >= 200 && status < 300 || status === 304) && stopped === false) {
400
- d.resolve(response[2][settings.dataType], "success", d)
325
+ if ((status >= 200 && status < 300 || status === 304) && stopped === false) {
326
+ d.resolve(response[2][settings.dataType])
401
327
  } else {
402
328
  // TODO probably resolve better
403
329
  d.reject(d, 'error', response[1]);
404
330
  }
405
331
  },
406
- // get the result form the fixture
407
- result = settings.fixture(settings, success, settings.headers, settings);
408
- if(result !== undefined) {
409
- d.resolve(result, "success", d)
332
+ // get the result form the fixture
333
+ result = settings.fixture(settings, success, settings.headers, settings);
334
+ if (result !== undefined) {
335
+ d.resolve(result)
410
336
  }
411
337
  }, can.fixture.delay);
412
-
338
+
413
339
  return d;
414
340
  } else {
415
341
  return AJAX(settings);
@@ -417,10 +343,10 @@ var compareMethods = {
417
343
  }
418
344
  }
419
345
 
420
- var typeTest = /^(script|json|test|jsonp)$/,
421
- // a list of 'overwrite' settings object
346
+ var typeTest = /^(script|json|text|jsonp)$/,
347
+ // a list of 'overwrite' settings object
422
348
  overwrites = [],
423
- // returns the index of an overwrite function
349
+ // returns the index of an overwrite function
424
350
  find = function (settings, exact) {
425
351
  for (var i = 0; i < overwrites.length; i++) {
426
352
  if ($fixture._similar(settings, overwrites[i], exact)) {
@@ -429,7 +355,7 @@ var compareMethods = {
429
355
  }
430
356
  return -1;
431
357
  },
432
- // overwrites the settings fixture if an overwrite matches
358
+ // overwrites the settings fixture if an overwrite matches
433
359
  overwrite = function (settings) {
434
360
  var index = find(settings);
435
361
  if (index > -1) {
@@ -438,24 +364,15 @@ var compareMethods = {
438
364
  }
439
365
 
440
366
  },
441
- /**
442
- * Makes an attempt to guess where the id is at in the url and returns it.
443
- * @param {Object} settings
444
- */
445
- getId = function (settings) {
367
+
368
+ getId = function (settings) {
446
369
  var id = settings.data.id;
447
370
 
448
371
  if (id === undefined && typeof settings.data === "number") {
449
372
  id = settings.data;
450
373
  }
451
374
 
452
- /*
453
- Check for id in params(if query string)
454
- If this is just a string representation of an id, parse
455
- if(id === undefined && typeof settings.data === "string") {
456
- id = settings.data;
457
- }
458
- //*/
375
+
459
376
 
460
377
  if (id === undefined) {
461
378
  settings.url.replace(/\/(\d+)(\/|$|\.)/g, function (all, num) {
@@ -478,36 +395,7 @@ var compareMethods = {
478
395
  return id;
479
396
  };
480
397
 
481
- /**
482
- * @plugin can/util/fixture
483
- * @test can/util/fixture/qunit.html
484
- *
485
- * `can.fixture` intercept an AJAX request and simulates the response with a file or function.
486
- * Read more about the usage in the [overview can.fixture].
487
- *
488
- * @param {Object|String} settings Configures the AJAX requests the fixture should
489
- * intercept. If an __object__ is passed, the object's properties and values
490
- * are matched against the settings passed to can.ajax.
491
- *
492
- * If a __string__ is passed, it can be used to match the url and type. Urls
493
- * can be templated, using <code>{NAME}</code> as wildcards.
494
- *
495
- * @param {Function|String} fixture The response to use for the AJAX
496
- * request. If a __string__ url is passed, the ajax request is redirected
497
- * to the url. If a __function__ is provided, it looks like:
498
- *
499
- * fixture( originalSettings, settings, callback, headers)
500
- *
501
- * where:
502
- *
503
- * - originalSettings - the orignal settings passed to can.ajax
504
- * - settings - the settings after all filters have run
505
- * - callback - a callback to call with the response if the fixture executes asynchronously
506
- * - headers - request headers
507
- *
508
- * If __null__ is passed, and there is a fixture at settings, that fixture will be removed,
509
- * allowing the AJAX request to behave normally.
510
- */
398
+
511
399
  var $fixture = can.fixture = function (settings, fixture) {
512
400
  // if we provide a fixture ...
513
401
  if (fixture !== undefined) {
@@ -516,19 +404,19 @@ var compareMethods = {
516
404
  var matches = settings.match(/(GET|POST|PUT|DELETE) (.+)/i);
517
405
  if (!matches) {
518
406
  settings = {
519
- url : settings
407
+ url: settings
520
408
  };
521
409
  } else {
522
410
  settings = {
523
- url : matches[2],
524
- type : matches[1]
411
+ url: matches[2],
412
+ type: matches[1]
525
413
  };
526
414
  }
527
415
 
528
416
  }
529
417
 
530
418
  //handle removing. An exact match if fixture was provided, otherwise, anything similar
531
- var index = find(settings, !!fixture);
419
+ var index = find(settings, !! fixture);
532
420
  if (index > -1) {
533
421
  overwrites.splice(index, 1)
534
422
  }
@@ -538,7 +426,7 @@ var compareMethods = {
538
426
  settings.fixture = fixture;
539
427
  overwrites.push(settings)
540
428
  } else {
541
- can.each(settings, function(fixture, url){
429
+ can.each(settings, function (fixture, url) {
542
430
  $fixture(url, fixture);
543
431
  })
544
432
  }
@@ -547,22 +435,24 @@ var compareMethods = {
547
435
 
548
436
  can.extend(can.fixture, {
549
437
  // given ajax settings, find an overwrite
550
- _similar : function (settings, overwrite, exact) {
438
+ _similar: function (settings, overwrite, exact) {
551
439
  if (exact) {
552
- return can.Object.same(settings, overwrite, {fixture : null})
440
+ return can.Object.same(settings, overwrite, {
441
+ fixture: null
442
+ })
553
443
  } else {
554
444
  return can.Object.subset(settings, overwrite, can.fixture._compare)
555
445
  }
556
446
  },
557
- _compare : {
558
- url : function (a, b) {
447
+ _compare: {
448
+ url: function (a, b) {
559
449
  return !!$fixture._getData(b, a)
560
450
  },
561
- fixture : null,
562
- type : "i"
451
+ fixture: null,
452
+ type: "i"
563
453
  },
564
454
  // gets data from a url like "/todo/{id}" given "todo/5"
565
- _getData : function (fixtureUrl, url) {
455
+ _getData: function (fixtureUrl, url) {
566
456
  var order = [],
567
457
  fixtureUrlAdjusted = fixtureUrl.replace('.', '\\.').replace('?', '\\?'),
568
458
  res = new RegExp(fixtureUrlAdjusted.replace(replacer, function (whole, part) {
@@ -581,95 +471,10 @@ var compareMethods = {
581
471
  return data;
582
472
  },
583
473
 
584
- make : function (types, count, make, filter) {
585
- /**
586
- * @function can.fixture.make
587
- * @parent can.fixture
588
- *
589
- * `can.fixture.make` is used for findAll / findOne style requests.
590
- *
591
- * ## With can.ajax
592
- *
593
- * //makes a nested list of messages
594
- * can.fixture.make(["messages","message"], 1000,
595
- * function(i, messages){
596
- * return {
597
- * subject: "This is message "+i,
598
- * body: "Here is some text for this message",
599
- * date: Math.floor( new Date().getTime() ),
600
- * parentId : i < 100 ? null : Math.floor(Math.random()*i)
601
- * }
602
- * })
603
- * //uses the message fixture to return messages limited by
604
- * // offset, limit, order, etc.
605
- * can.ajax({
606
- * url: "messages",
607
- * data: {
608
- * offset: 100,
609
- * limit: 50,
610
- * order: ["date ASC"],
611
- * parentId: 5},
612
- * },
613
- * fixture: "-messages",
614
- * success: function( messages ) { ... }
615
- * });
616
- *
617
- * ## With can.Model
618
- *
619
- * `can.fixture.make` returns a model store that offers `findAll`, `findOne`, `create`,
620
- * `update` and `destroy` fixture functions you can map to a [can.Model] Ajax request.
621
- * Consider a model like this:
622
- *
623
- * var Todo = can.Model({
624
- * findAll : 'GET /todos',
625
- * findOne : 'GET /todos/{id}',
626
- * create : 'POST /todos',
627
- * update : 'PUT /todos/{id}',
628
- * destroy : 'DELETE /todos/{id}'
629
- * }, {});
630
- *
631
- * And an unnamed generated fixture like this:
632
- *
633
- * var store = can.fixture.make(100, function(i) {
634
- * return {
635
- * id : i,
636
- * name : 'Todo ' + i
637
- * }
638
- * });
639
- *
640
- * You can map can.Model requests using the return value of `can.fixture.make`:
641
- *
642
- * can.fixture('GET /todos', store.findAll);
643
- * can.fixture('GET /todos/{id}', store.findOne);
644
- * can.fixture('POST /todos', store.create);
645
- * can.fixture('PUT /todos/{id}', store.update);
646
- * can.fixture('DELETE /todos/{id}', store.destroy);
647
- *
648
- * @param {Array|String} types An array of the fixture names or the singular fixture name.
649
- * If an array, the first item is the plural fixture name (prefixed with -) and the second
650
- * item is the singular name. If a string, it's assumed to be the singular fixture name. Make
651
- * will simply add s to the end of it for the plural name. If this parameter is not an array
652
- * or a String the fixture won't be added and only return the generator object.
653
- * @param {Number} count the number of items to create
654
- * @param {Function} make a function that will return the JavaScript object. The
655
- * make function is called back with the id and the current array of items.
656
- * @param {Function} filter (optional) a function used to further filter results. Used for to simulate
657
- * server params like searchText or startDate.
658
- * The function should return true if the item passes the filter,
659
- * false otherwise. For example:
660
- *
661
- *
662
- * function(item, settings){
663
- * if(settings.data.searchText){
664
- * var regex = new RegExp("^"+settings.data.searchText)
665
- * return regex.test(item.name);
666
- * }
667
- * }
668
- *
669
- * @return {Object} A generator object providing fixture functions for *findAll*, *findOne*, *create*,
670
- * *update* and *destroy*.
671
- */
672
- var items = [], // TODO: change this to a hash
474
+ make: function (types, count, make, filter) {
475
+
476
+ var items = [],
477
+ // TODO: change this to a hash
673
478
  findOne = function (id) {
674
479
  for (var i = 0; i < items.length; i++) {
675
480
  if (id == items[i].id) {
@@ -680,7 +485,7 @@ var compareMethods = {
680
485
  methods = {};
681
486
 
682
487
  if (typeof types === "string") {
683
- types = [types + "s", types ]
488
+ types = [types + "s", types]
684
489
  } else if (!can.isArray(types)) {
685
490
  filter = make;
686
491
  make = count;
@@ -689,7 +494,7 @@ var compareMethods = {
689
494
 
690
495
  // make all items
691
496
  can.extend(methods, {
692
- findAll : function (settings) {
497
+ findAll: function (settings) {
693
498
  //copy array of items
694
499
  var retArr = items.slice(0);
695
500
  settings.data = settings.data || {};
@@ -727,7 +532,6 @@ var compareMethods = {
727
532
  });
728
533
  });
729
534
 
730
-
731
535
  var offset = parseInt(settings.data.offset, 10) || 0,
732
536
  limit = parseInt(settings.data.limit, 10) || (items.length - offset),
733
537
  i = 0;
@@ -736,7 +540,7 @@ var compareMethods = {
736
540
  for (var param in settings.data) {
737
541
  i = 0;
738
542
  if (settings.data[param] !== undefined && // don't do this if the value of the param is null (ignore it)
739
- (param.indexOf("Id") != -1 || param.indexOf("_id") != -1)) {
543
+ (param.indexOf("Id") != -1 || param.indexOf("_id") != -1)) {
740
544
  while (i < retArr.length) {
741
545
  if (settings.data[param] != retArr[i][param]) {
742
546
  retArr.splice(i, 1);
@@ -760,28 +564,28 @@ var compareMethods = {
760
564
 
761
565
  //return data spliced with limit and offset
762
566
  return {
763
- "count" : retArr.length,
764
- "limit" : settings.data.limit,
765
- "offset" : settings.data.offset,
766
- "data" : retArr.slice(offset, offset + limit)
567
+ "count": retArr.length,
568
+ "limit": settings.data.limit,
569
+ "offset": settings.data.offset,
570
+ "data": retArr.slice(offset, offset + limit)
767
571
  };
768
572
  },
769
- findOne : function (orig, response) {
573
+ findOne: function (orig, response) {
770
574
  var item = findOne(getId(orig));
771
575
  response(item ? item : undefined);
772
576
  },
773
- update : function (orig,response) {
577
+ update: function (orig, response) {
774
578
  var id = getId(orig);
775
579
 
776
580
  // TODO: make it work with non-linear ids ..
777
581
  can.extend(findOne(id), orig.data);
778
582
  response({
779
- id : getId(orig)
583
+ id: getId(orig)
780
584
  }, {
781
- location : orig.url + "/" + getId(orig)
585
+ location: orig.url + "/" + getId(orig)
782
586
  });
783
587
  },
784
- destroy : function (settings) {
588
+ destroy: function (settings) {
785
589
  var id = getId(settings);
786
590
  for (var i = 0; i < items.length; i++) {
787
591
  if (items[i].id == id) {
@@ -794,7 +598,7 @@ var compareMethods = {
794
598
  can.extend(findOne(id) || {}, settings.data);
795
599
  return {};
796
600
  },
797
- create : function (settings, response) {
601
+ create: function (settings, response) {
798
602
  var item = make(items.length, items);
799
603
 
800
604
  can.extend(item, settings.data);
@@ -806,9 +610,9 @@ var compareMethods = {
806
610
  items.push(item);
807
611
  var id = item.id || parseInt(Math.random() * 100000, 10);
808
612
  response({
809
- id : id
613
+ id: id
810
614
  }, {
811
- location : settings.url + "/" + id
615
+ location: settings.url + "/" + id
812
616
  })
813
617
  }
814
618
  });
@@ -824,62 +628,24 @@ var compareMethods = {
824
628
  }
825
629
 
826
630
  // if we have types given add them to can.fixture
827
- if(can.isArray(types)) {
631
+ if (can.isArray(types)) {
828
632
  can.fixture["~" + types[0]] = items;
829
633
  can.fixture["-" + types[0]] = methods.findAll;
830
634
  can.fixture["-" + types[1]] = methods.findOne;
831
- can.fixture["-" + types[1]+"Update"] = methods.update;
832
- can.fixture["-" + types[1]+"Destroy"] = methods.destroy;
833
- can.fixture["-" + types[1]+"Create"] = methods.create;
635
+ can.fixture["-" + types[1] + "Update"] = methods.update;
636
+ can.fixture["-" + types[1] + "Destroy"] = methods.destroy;
637
+ can.fixture["-" + types[1] + "Create"] = methods.create;
834
638
  }
835
639
 
836
640
  return can.extend({
837
641
  getId: getId,
838
- find : function(settings){
839
- return findOne( getId(settings) );
642
+ find: function (settings) {
643
+ return findOne(getId(settings));
840
644
  }
841
645
  }, methods);
842
646
  },
843
- /**
844
- * @function can.fixture.rand
845
- * @parent can.fixture
846
- *
847
- * `can.fixture.rand` creates random integers or random arrays of
848
- * other arrays.
849
- *
850
- * ## Examples
851
- *
852
- * var rand = can.fixture.rand;
853
- *
854
- * // get a random integer between 0 and 10 (inclusive)
855
- * rand(11);
856
- *
857
- * // get a random number between -5 and 5 (inclusive)
858
- * rand(-5, 6);
859
- *
860
- * // pick a random item from an array
861
- * rand(["j","m","v","c"],1)[0]
862
- *
863
- * // pick a random number of items from an array
864
- * rand(["j","m","v","c"])
865
- *
866
- * // pick 2 items from an array
867
- * rand(["j","m","v","c"],2)
868
- *
869
- * // pick between 2 and 3 items at random
870
- * rand(["j","m","v","c"],2,3)
871
- *
872
- *
873
- * @param {Array|Number} arr An array of items to select from.
874
- * If a number is provided, a random number is returned.
875
- * If min and max are not provided, a random number of items are selected
876
- * from this array.
877
- * @param {Number} [min] If only min is provided, min items
878
- * are selected.
879
- * @param {Number} [max] If min and max are provided, a random number of
880
- * items between min and max (inclusive) is selected.
881
- */
882
- rand : function (arr, min, max) {
647
+
648
+ rand: function (arr, min, max) {
883
649
  if (typeof arr == 'number') {
884
650
  if (typeof min == 'number') {
885
651
  return arr + Math.floor(Math.random() * (min - arr));
@@ -907,95 +673,35 @@ var compareMethods = {
907
673
  }
908
674
  return res;
909
675
  },
910
- /**
911
- * @hide
912
- *
913
- * Use can.fixture.xhr to create an object that looks like an xhr object.
914
- *
915
- * ## Example
916
- *
917
- * The following example shows how the -restCreate fixture uses xhr to return
918
- * a simulated xhr object:
919
- * @codestart
920
- * "-restCreate" : function( settings, cbType ) {
921
- * switch(cbType){
922
- * case "success":
923
- * return [
924
- * {id: parseInt(Math.random()*1000)},
925
- * "success",
926
- * can.fixture.xhr()];
927
- * case "complete":
928
- * return [
929
- * can.fixture.xhr({
930
- * getResponseHeader: function() {
931
- * return settings.url+"/"+parseInt(Math.random()*1000);
932
- * }
933
- * }),
934
- * "success"];
935
- * }
936
- * }
937
- * @codeend
938
- * @param {Object} [xhr] properties that you want to overwrite
939
- * @return {Object} an object that looks like a successful XHR object.
940
- */
941
- xhr : function (xhr) {
676
+
677
+ xhr: function (xhr) {
942
678
  return can.extend({}, {
943
- abort : can.noop,
944
- getAllResponseHeaders : function () {
679
+ abort: can.noop,
680
+ getAllResponseHeaders: function () {
945
681
  return "";
946
682
  },
947
- getResponseHeader : function () {
683
+ getResponseHeader: function () {
948
684
  return "";
949
685
  },
950
- open : can.noop,
951
- overrideMimeType : can.noop,
952
- readyState : 4,
953
- responseText : "",
954
- responseXML : null,
955
- send : can.noop,
956
- setRequestHeader : can.noop,
957
- status : 200,
958
- statusText : "OK"
686
+ open: can.noop,
687
+ overrideMimeType: can.noop,
688
+ readyState: 4,
689
+ responseText: "",
690
+ responseXML: null,
691
+ send: can.noop,
692
+ setRequestHeader: can.noop,
693
+ status: 200,
694
+ statusText: "OK"
959
695
  }, xhr);
960
696
  },
961
- /**
962
- * @attribute can.fixture.on
963
- * @parent can.fixture
964
- *
965
- * `can.fixture.on` lets you programatically turn off fixtures. This is mostly used for testing.
966
- *
967
- * can.fixture.on = false
968
- * Task.findAll({}, function(){
969
- * can.fixture.on = true;
970
- * })
971
- */
972
- on : true
697
+
698
+ on: true
973
699
  });
974
- /**
975
- * @attribute can.fixture.delay
976
- * @parent can.fixture
977
- *
978
- * `can.fixture.delay` indicates the delay in milliseconds between an ajax request is made and
979
- * the success and complete handlers are called. This only sets
980
- * functional synchronous fixtures that return a result. By default, the delay is 200ms.
981
- *
982
- * @codestart
983
- * steal('can/util/fixtures').then(function(){
984
- * can.fixture.delay = 1000;
985
- * })
986
- * @codeend
987
- */
700
+
988
701
  can.fixture.delay = 200;
989
702
 
990
- /**
991
- * @attribute can.fixture.rootUrl
992
- * @parent can.fixture
993
- *
994
- * `can.fixture.rootUrl` contains the root URL for fixtures to use.
995
- * If you are using StealJS it will use the Steal root
996
- * URL by default.
997
- */
998
- can.fixture.rootUrl = window.steal ? steal.root : undefined;
703
+
704
+ can.fixture.rootUrl = getUrl('');
999
705
 
1000
706
  can.fixture["-handleFunction"] = function (settings) {
1001
707
  if (typeof settings.fixture === "string" && can.fixture[settings.fixture]) {
@@ -1017,4 +723,5 @@ var compareMethods = {
1017
723
 
1018
724
  //Expose this for fixture debugging
1019
725
  can.fixture.overwrites = overwrites;
1020
- })(this.can, this )
726
+
727
+ })(can, this);