diff --git a/bower.json b/bower.json index 08bf043f..e2f5b9ee 100644 --- a/bower.json +++ b/bower.json @@ -2,7 +2,7 @@ "name": "vue-resource", "main": "dist/vue-resource.js", "description": "A web request service for Vue.js", - "version": "0.1.5", + "version": "0.1.6", "homepage": "https://github.com/vuejs/vue-resource", "license": "MIT", "ignore": [ diff --git a/dist/vue-resource.js b/dist/vue-resource.js index 8dc18717..e071f77a 100644 --- a/dist/vue-resource.js +++ b/dist/vue-resource.js @@ -1,5 +1,5 @@ /** - * vue-resource v0.1.5 + * vue-resource v0.1.6 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ @@ -102,7 +102,7 @@ return ''; }); - if (options.root !== false && !url.match(/^(https?:)?\//)) { + if (typeof options.root === 'string' && !url.match(/^(https?:)?\//)) { url = options.root + '/' + url; } @@ -130,7 +130,6 @@ Url.options = { url: '', - root: false, params: {} }; @@ -355,40 +354,36 @@ options.data = ''; } - promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options); + promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options).then(transformResponse, transformResponse); - _.extend(promise, { - - success: function (onSuccess) { - - this.then(function (request) { - onSuccess.apply(self, parseReq(request)); - }, function () {}); + promise.success = function (fn) { - return this; - }, + promise.then(function (response) { + fn.call(self, response.data, response.status, response); + }); - error: function (onError) { + return promise; + }; - this.catch(function (request) { - onError.apply(self, parseReq(request)); - }); + promise.error = function (fn) { - return this; - }, + promise.catch(function (response) { + fn.call(self, response.data, response.status, response); + }); - always: function (onAlways) { + return promise; + }; - var cb = function (request) { - onAlways.apply(self, parseReq(request)); - }; + promise.always = function (fn) { - this.then(cb, cb); + var cb = function (response) { + fn.call(self, response.data, response.status, response); + }; - return this; - } + promise.then(cb, cb); - }); + return promise; + }; if (options.success) { promise.success(options.success); @@ -505,17 +500,15 @@ return promise; } - function parseReq(request) { - - var result; + function transformResponse(response) { try { - result = JSON.parse(request.responseText); + response.data = JSON.parse(response.responseText); } catch (e) { - result = request.responseText; + response.data = response.responseText; } - return [result, request.status, request]; + return response; } Http.options = { @@ -525,7 +518,7 @@ jsonp: 'callback', beforeSend: null, emulateHTTP: false, - emulateJSON: false, + emulateJSON: false }; Http.headers = { diff --git a/dist/vue-resource.min.js b/dist/vue-resource.min.js index 833bf84a..b3025750 100644 --- a/dist/vue-resource.min.js +++ b/dist/vue-resource.min.js @@ -1,7 +1,7 @@ /** - * vue-resource v0.1.5 + * vue-resource v0.1.6 * https://github.com/vuejs/vue-resource * Released under the MIT License. */ -!function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){function r(t){t.url=n(1)(t),t.http=n(3)(t),t.resource=n(5)(t)}window.Vue&&Vue.use(r),t.exports=r},function(t,e,n){t.exports=function(t){function e(t,n){var r,s={},i={},c=t;return a.isPlainObject(c)||(c={url:t,params:n}),c=a.extend({},e.options,a.options("url",this,c)),t=c.url.replace(/:([a-z]\w*)/gi,function(t,e){return c.params[e]?(s[e]=!0,o(c.params[e])):""}),c.root===!1||t.match(/^(https?:)?\//)||(t=c.root+"/"+t),t=t.replace(/([^:])[\/]{2,}/g,"$1/"),t=t.replace(/(\w+)\/+$/,"$1"),a.each(c.params,function(t,e){s[e]||(i[e]=t)}),r=e.params(i),r&&(t+=(-1==t.indexOf("?")?"?":"&")+r),t}function r(t,e,n){var o,s=a.isArray(e),i=a.isPlainObject(e);a.each(e,function(e,c){o=a.isObject(e)||a.isArray(e),n&&(c=n+"["+(i||o?c:"")+"]"),!n&&s?t.add(e.name,e.value):o?r(t,e,c):t.add(c,e)})}function o(t){return s(t,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function s(t,e){return encodeURIComponent(t).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,e?"%20":"+")}var a=n(2)(t);return e.options={url:"",root:!1,params:{}},e.params=function(t){var e=[];return e.add=function(t,e){a.isFunction(e)&&(e=e()),null===e&&(e=""),this.push(o(t)+"="+o(e))},r(e,t),e.join("&")},e.parse=function(t){var e=new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),n=t.match(e);return{url:t,scheme:n[1]||"",host:n[2]||"",path:n[3]||"",query:n[4]||"",fragment:n[5]||""}},Object.defineProperty(t.prototype,"$url",{get:function(){return a.extend(e.bind(this),e)}}),e}},function(t,e){t.exports=function(t){function e(t,r,o){for(var s in r)o&&(n.isPlainObject(r[s])||n.isArray(r[s]))?(n.isPlainObject(r[s])&&!n.isPlainObject(t[s])&&(t[s]={}),n.isArray(r[s])&&!n.isArray(t[s])&&(t[s]=[]),e(t[s],r[s],o)):void 0!==r[s]&&(t[s]=r[s])}var n=t.util.extend({},t.util);return n.options=function(t,e,r){var o=e.$options||{};return n.extend({},o[t],r)},n.each=function(t,e){var r,o;if("number"==typeof t.length)for(r=0;r=200&&this.status<300?r(this):o(this))},n.send(e.data)});return a.extend(r,{abort:function(){n.abort()}}),r}function o(t,e){var n,r,o="_jsonp"+Math.random().toString(36).substr(2);e.params[e.jsonp]=o,a.isFunction(e.beforeSend)&&e.beforeSend({},e);var s=new i(function(s,a){n=document.createElement("script"),n.src=t(e.url,e.params),n.type="text/javascript",n.async=!0,window[o]=function(t){r=t};var i=function(t){delete window[o],document.body.removeChild(n),"load"!==t.type||r||(t.type="error");var e=r?r:t.type,i="error"===t.type?404:200;(200===i?s:a)({responseText:e,status:i})};n.onload=i,n.onerror=i,document.body.appendChild(n)});return s}function s(t){var e;try{e=JSON.parse(t.responseText)}catch(n){e=t.responseText}return[e,t.status,t]}var a=n(2)(t),i=n(4),c={"Content-Type":"application/json;charset=utf-8"};return e.options={method:"get",params:{},data:"",jsonp:"callback",beforeSend:null,emulateHTTP:!1,emulateJSON:!1},e.headers={put:c,post:c,patch:c,"delete":c,common:{Accept:"application/json, text/plain, */*","X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(t){e[t]=function(e,n,r,o){return a.isFunction(n)&&(o=r,r=n,n=void 0),this(e,a.extend({method:t,data:n,success:r},o))}}),Object.defineProperty(t.prototype,"$http",{get:function(){return a.extend(e.bind(this),e)}}),e}},function(t,e){function n(t){t(this.resolve.bind(this),this.reject.bind(this)),this._thens=[]}n.prototype={then:function(t,e,n){this._thens.push({resolve:t,reject:e,progress:n})},"catch":function(t){this._thens.push({reject:t})},resolve:function(t){this._complete("resolve",t)},reject:function(t){this._complete("reject",t)},progress:function(t){for(var e,n=0;e=this._thens[n++];)e.progress&&e.progress(t)},_complete:function(t,e){this.then="resolve"===t?function(t,n){t&&t(e)}:function(t,n){n&&n(e)},this.resolve=this.reject=this.progress=function(){throw new Error("Promise already completed.")};for(var n,r=0;n=this._thens[r++];)n[t]&&n[t](e);delete this._thens}},t.exports=window.Promise?window.Promise:n},function(t,e,n){t.exports=function(t){function e(n,s,a){var i=this,c={};return a=o.extend({},e.actions,a),o.each(a,function(e,a){e=o.extend(!0,{url:n,params:s||{}},e),c[a]=function(){return(i.$http||t.http)(r(e,arguments))}}),c}function r(t,e){var n,r,s,a=o.extend({},t),i={};switch(e.length){case 4:s=e[3],r=e[2];case 3:case 2:if(!o.isFunction(e[1])){i=e[0],n=e[1],r=e[2];break}if(o.isFunction(e[0])){r=e[0],s=e[1];break}r=e[1],s=e[2];case 1:o.isFunction(e[0])?r=e[0]:/^(POST|PUT|PATCH)$/i.test(a.method)?n=e[0]:i=e[0];break;case 0:break;default:throw"Expected up to 4 arguments [params, data, success, error], got "+e.length+" arguments"}return a.url=t.url,a.data=n,a.params=o.extend({},t.params,i),r&&(a.success=r),s&&(a.error=s),a}var o=n(2)(t);return e.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},remove:{method:"DELETE"},"delete":{method:"DELETE"}},Object.defineProperty(t.prototype,"$resource",{get:function(){return e.bind(this)}}),e}}]); \ No newline at end of file +!function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){function r(t){t.url=n(1)(t),t.http=n(3)(t),t.resource=n(5)(t)}window.Vue&&Vue.use(r),t.exports=r},function(t,e,n){t.exports=function(t){function e(t,n){var r,a={},i={},c=t;return s.isPlainObject(c)||(c={url:t,params:n}),c=s.extend({},e.options,s.options("url",this,c)),t=c.url.replace(/:([a-z]\w*)/gi,function(t,e){return c.params[e]?(a[e]=!0,o(c.params[e])):""}),"string"!=typeof c.root||t.match(/^(https?:)?\//)||(t=c.root+"/"+t),t=t.replace(/([^:])[\/]{2,}/g,"$1/"),t=t.replace(/(\w+)\/+$/,"$1"),s.each(c.params,function(t,e){a[e]||(i[e]=t)}),r=e.params(i),r&&(t+=(-1==t.indexOf("?")?"?":"&")+r),t}function r(t,e,n){var o,a=s.isArray(e),i=s.isPlainObject(e);s.each(e,function(e,c){o=s.isObject(e)||s.isArray(e),n&&(c=n+"["+(i||o?c:"")+"]"),!n&&a?t.add(e.name,e.value):o?r(t,e,c):t.add(c,e)})}function o(t){return a(t,!0).replace(/%26/gi,"&").replace(/%3D/gi,"=").replace(/%2B/gi,"+")}function a(t,e){return encodeURIComponent(t).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,e?"%20":"+")}var s=n(2)(t);return e.options={url:"",params:{}},e.params=function(t){var e=[];return e.add=function(t,e){s.isFunction(e)&&(e=e()),null===e&&(e=""),this.push(o(t)+"="+o(e))},r(e,t),e.join("&")},e.parse=function(t){var e=new RegExp("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?"),n=t.match(e);return{url:t,scheme:n[1]||"",host:n[2]||"",path:n[3]||"",query:n[4]||"",fragment:n[5]||""}},Object.defineProperty(t.prototype,"$url",{get:function(){return s.extend(e.bind(this),e)}}),e}},function(t,e){t.exports=function(t){function e(t,r,o){for(var a in r)o&&(n.isPlainObject(r[a])||n.isArray(r[a]))?(n.isPlainObject(r[a])&&!n.isPlainObject(t[a])&&(t[a]={}),n.isArray(r[a])&&!n.isArray(t[a])&&(t[a]=[]),e(t[a],r[a],o)):void 0!==r[a]&&(t[a]=r[a])}var n=t.util.extend({},t.util);return n.options=function(t,e,r){var o=e.$options||{};return n.extend({},o[t],r)},n.each=function(t,e){var r,o;if("number"==typeof t.length)for(r=0;r=200&&this.status<300?r(this):o(this))},n.send(e.data)});return s.extend(r,{abort:function(){n.abort()}}),r}function o(t,e){var n,r,o="_jsonp"+Math.random().toString(36).substr(2);e.params[e.jsonp]=o,s.isFunction(e.beforeSend)&&e.beforeSend({},e);var a=new i(function(a,s){n=document.createElement("script"),n.src=t(e.url,e.params),n.type="text/javascript",n.async=!0,window[o]=function(t){r=t};var i=function(t){delete window[o],document.body.removeChild(n),"load"!==t.type||r||(t.type="error");var e=r?r:t.type,i="error"===t.type?404:200;(200===i?a:s)({responseText:e,status:i})};n.onload=i,n.onerror=i,document.body.appendChild(n)});return a}function a(t){try{t.data=JSON.parse(t.responseText)}catch(e){t.data=t.responseText}return t}var s=n(2)(t),i=n(4),c={"Content-Type":"application/json;charset=utf-8"};return e.options={method:"get",params:{},data:"",jsonp:"callback",beforeSend:null,emulateHTTP:!1,emulateJSON:!1},e.headers={put:c,post:c,patch:c,"delete":c,common:{Accept:"application/json, text/plain, */*","X-Requested-With":"XMLHttpRequest"}},["get","put","post","patch","delete","jsonp"].forEach(function(t){e[t]=function(e,n,r,o){return s.isFunction(n)&&(o=r,r=n,n=void 0),this(e,s.extend({method:t,data:n,success:r},o))}}),Object.defineProperty(t.prototype,"$http",{get:function(){return s.extend(e.bind(this),e)}}),e}},function(t,e){function n(t){t(this.resolve.bind(this),this.reject.bind(this)),this._thens=[]}n.prototype={then:function(t,e,n){this._thens.push({resolve:t,reject:e,progress:n})},"catch":function(t){this._thens.push({reject:t})},resolve:function(t){this._complete("resolve",t)},reject:function(t){this._complete("reject",t)},progress:function(t){for(var e,n=0;e=this._thens[n++];)e.progress&&e.progress(t)},_complete:function(t,e){this.then="resolve"===t?function(t,n){t&&t(e)}:function(t,n){n&&n(e)},this.resolve=this.reject=this.progress=function(){throw new Error("Promise already completed.")};for(var n,r=0;n=this._thens[r++];)n[t]&&n[t](e);delete this._thens}},t.exports=window.Promise?window.Promise:n},function(t,e,n){t.exports=function(t){function e(n,a,s){var i=this,c={};return s=o.extend({},e.actions,s),o.each(s,function(e,s){e=o.extend(!0,{url:n,params:a||{}},e),c[s]=function(){return(i.$http||t.http)(r(e,arguments))}}),c}function r(t,e){var n,r,a,s=o.extend({},t),i={};switch(e.length){case 4:a=e[3],r=e[2];case 3:case 2:if(!o.isFunction(e[1])){i=e[0],n=e[1],r=e[2];break}if(o.isFunction(e[0])){r=e[0],a=e[1];break}r=e[1],a=e[2];case 1:o.isFunction(e[0])?r=e[0]:/^(POST|PUT|PATCH)$/i.test(s.method)?n=e[0]:i=e[0];break;case 0:break;default:throw"Expected up to 4 arguments [params, data, success, error], got "+e.length+" arguments"}return s.url=t.url,s.data=n,s.params=o.extend({},t.params,i),r&&(s.success=r),a&&(s.error=a),s}var o=n(2)(t);return e.actions={get:{method:"GET"},save:{method:"POST"},query:{method:"GET"},remove:{method:"DELETE"},"delete":{method:"DELETE"}},Object.defineProperty(t.prototype,"$resource",{get:function(){return e.bind(this)}}),e}}]); \ No newline at end of file diff --git a/package.json b/package.json index 718d9321..2b6c66ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-resource", - "version": "0.1.5", + "version": "0.1.6", "description": "A web request service for Vue.js", "main": "src/index.js", "scripts": {