|
1 | 1 | /** |
2 | | - * vue-resource v0.1.5 |
| 2 | + * vue-resource v0.1.6 |
3 | 3 | * https://github.com/vuejs/vue-resource |
4 | 4 | * Released under the MIT License. |
5 | 5 | */ |
|
102 | 102 | return ''; |
103 | 103 | }); |
104 | 104 |
|
105 | | - if (options.root !== false && !url.match(/^(https?:)?\//)) { |
| 105 | + if (typeof options.root === 'string' && !url.match(/^(https?:)?\//)) { |
106 | 106 | url = options.root + '/' + url; |
107 | 107 | } |
108 | 108 |
|
|
130 | 130 |
|
131 | 131 | Url.options = { |
132 | 132 | url: '', |
133 | | - root: false, |
134 | 133 | params: {} |
135 | 134 | }; |
136 | 135 |
|
|
355 | 354 | options.data = ''; |
356 | 355 | } |
357 | 356 |
|
358 | | - promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options); |
| 357 | + promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options).then(transformResponse, transformResponse); |
359 | 358 |
|
360 | | - _.extend(promise, { |
361 | | - |
362 | | - success: function (onSuccess) { |
363 | | - |
364 | | - this.then(function (request) { |
365 | | - onSuccess.apply(self, parseReq(request)); |
366 | | - }, function () {}); |
| 359 | + promise.success = function (fn) { |
367 | 360 |
|
368 | | - return this; |
369 | | - }, |
| 361 | + promise.then(function (response) { |
| 362 | + fn.call(self, response.data, response.status, response); |
| 363 | + }); |
370 | 364 |
|
371 | | - error: function (onError) { |
| 365 | + return promise; |
| 366 | + }; |
372 | 367 |
|
373 | | - this.catch(function (request) { |
374 | | - onError.apply(self, parseReq(request)); |
375 | | - }); |
| 368 | + promise.error = function (fn) { |
376 | 369 |
|
377 | | - return this; |
378 | | - }, |
| 370 | + promise.catch(function (response) { |
| 371 | + fn.call(self, response.data, response.status, response); |
| 372 | + }); |
379 | 373 |
|
380 | | - always: function (onAlways) { |
| 374 | + return promise; |
| 375 | + }; |
381 | 376 |
|
382 | | - var cb = function (request) { |
383 | | - onAlways.apply(self, parseReq(request)); |
384 | | - }; |
| 377 | + promise.always = function (fn) { |
385 | 378 |
|
386 | | - this.then(cb, cb); |
| 379 | + var cb = function (response) { |
| 380 | + fn.call(self, response.data, response.status, response); |
| 381 | + }; |
387 | 382 |
|
388 | | - return this; |
389 | | - } |
| 383 | + promise.then(cb, cb); |
390 | 384 |
|
391 | | - }); |
| 385 | + return promise; |
| 386 | + }; |
392 | 387 |
|
393 | 388 | if (options.success) { |
394 | 389 | promise.success(options.success); |
|
505 | 500 | return promise; |
506 | 501 | } |
507 | 502 |
|
508 | | - function parseReq(request) { |
509 | | - |
510 | | - var result; |
| 503 | + function transformResponse(response) { |
511 | 504 |
|
512 | 505 | try { |
513 | | - result = JSON.parse(request.responseText); |
| 506 | + response.data = JSON.parse(response.responseText); |
514 | 507 | } catch (e) { |
515 | | - result = request.responseText; |
| 508 | + response.data = response.responseText; |
516 | 509 | } |
517 | 510 |
|
518 | | - return [result, request.status, request]; |
| 511 | + return response; |
519 | 512 | } |
520 | 513 |
|
521 | 514 | Http.options = { |
|
525 | 518 | jsonp: 'callback', |
526 | 519 | beforeSend: null, |
527 | 520 | emulateHTTP: false, |
528 | | - emulateJSON: false, |
| 521 | + emulateJSON: false |
529 | 522 | }; |
530 | 523 |
|
531 | 524 | Http.headers = { |
|
0 commit comments