Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.

Commit 7dbba59

Browse files
committed
update file structure
1 parent c0642f1 commit 7dbba59

File tree

9 files changed

+223
-209
lines changed

9 files changed

+223
-209
lines changed

src/http.js

Lines changed: 10 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
module.exports = function (Vue) {
1+
/**
2+
* Service for sending network requests.
3+
*/
24

3-
var _ = require('./util')(Vue);
4-
var Promise = require('./promise');
5-
var jsonType = { 'Content-Type': 'application/json;charset=utf-8' };
5+
var _ = require('./lib/util');
6+
var xhr = require('./lib/xhr');
7+
var jsonp = require('./lib/jsonp');
8+
var jsonType = {'Content-Type': 'application/json;charset=utf-8'};
69

7-
/**
8-
* Http provides a service for sending XMLHttpRequests.
9-
*/
10+
module.exports = function (Vue) {
1011

11-
function Http (url, options) {
12+
function Http(url, options) {
1213

1314
var self = this, headers, promise;
1415

@@ -30,7 +31,7 @@ module.exports = function (Vue) {
3031

3132
if (_.isPlainObject(options.data) && /^(get|jsonp)$/i.test(options.method)) {
3233
_.extend(options.params, options.data);
33-
options.data = '';
34+
delete options.data;
3435
}
3536

3637
promise = (options.method.toLowerCase() == 'jsonp' ? jsonp : xhr).call(this, this.$url || Vue.url, options);
@@ -77,110 +78,6 @@ module.exports = function (Vue) {
7778
return promise;
7879
}
7980

80-
function xhr(url, options) {
81-
82-
var request = new XMLHttpRequest();
83-
84-
if (_.isFunction(options.beforeSend)) {
85-
options.beforeSend(request, options);
86-
}
87-
88-
if (options.emulateHTTP && /^(put|patch|delete)$/i.test(options.method)) {
89-
options.headers['X-HTTP-Method-Override'] = options.method;
90-
options.method = 'post';
91-
}
92-
93-
if (options.emulateJSON && _.isPlainObject(options.data)) {
94-
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
95-
options.data = url.params(options.data);
96-
}
97-
98-
if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
99-
delete options.headers['Content-Type'];
100-
}
101-
102-
if (_.isPlainObject(options.data)) {
103-
options.data = JSON.stringify(options.data);
104-
}
105-
106-
var promise = new Promise(function (resolve, reject) {
107-
108-
request.open(options.method, url(options), true);
109-
110-
_.each(options.headers, function (value, header) {
111-
request.setRequestHeader(header, value);
112-
});
113-
114-
request.onreadystatechange = function () {
115-
116-
if (this.readyState === 4) {
117-
118-
if (this.status >= 200 && this.status < 300) {
119-
resolve(this);
120-
} else {
121-
reject(this);
122-
}
123-
}
124-
};
125-
126-
request.send(options.data);
127-
});
128-
129-
_.extend(promise, {
130-
131-
abort: function () {
132-
request.abort();
133-
}
134-
135-
});
136-
137-
return promise;
138-
}
139-
140-
function jsonp(url, options) {
141-
142-
var callback = '_jsonp' + Math.random().toString(36).substr(2), script, result;
143-
144-
options.params[options.jsonp] = callback;
145-
146-
if (_.isFunction(options.beforeSend)) {
147-
options.beforeSend({}, options);
148-
}
149-
150-
var promise = new Promise(function (resolve, reject) {
151-
152-
script = document.createElement('script');
153-
script.src = url(options.url, options.params);
154-
script.type = 'text/javascript';
155-
script.async = true;
156-
157-
window[callback] = function (data) {
158-
result = data;
159-
};
160-
161-
var handler = function (event) {
162-
163-
delete window[callback];
164-
document.body.removeChild(script);
165-
166-
if (event.type === 'load' && !result) {
167-
event.type = 'error';
168-
}
169-
170-
var text = result ? result : event.type, status = event.type === 'error' ? 404 : 200;
171-
172-
(status === 200 ? resolve : reject)({ responseText: text, status: status });
173-
};
174-
175-
script.onload = handler;
176-
script.onerror = handler;
177-
178-
document.body.appendChild(script);
179-
});
180-
181-
return promise;
182-
}
183-
18481
function transformResponse(response) {
18582

18683
try {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Install plugin.
33
*/
44

5-
function install (Vue) {
5+
function install(Vue) {
66
Vue.url = require('./url')(Vue);
77
Vue.http = require('./http')(Vue);
88
Vue.resource = require('./resource')(Vue);

src/lib/jsonp.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* JSONP request.
3+
*/
4+
5+
var _ = require('./util');
6+
var Promise = require('./promise');
7+
8+
module.exports = function (url, options) {
9+
10+
var callback = '_jsonp' + Math.random().toString(36).substr(2), script, body;
11+
12+
options.params[options.jsonp] = callback;
13+
14+
if (_.isFunction(options.beforeSend)) {
15+
options.beforeSend.call(this, {}, options);
16+
}
17+
18+
return new Promise(function (resolve, reject) {
19+
20+
script = document.createElement('script');
21+
script.src = url(options.url, options.params);
22+
script.type = 'text/javascript';
23+
script.async = true;
24+
25+
window[callback] = function (data) {
26+
body = data;
27+
};
28+
29+
var handler = function (event) {
30+
31+
delete window[callback];
32+
document.body.removeChild(script);
33+
34+
if (event.type === 'load' && !body) {
35+
event.type = 'error';
36+
}
37+
38+
var text = body ? body : event.type, status = event.type === 'error' ? 404 : 200;
39+
40+
(status === 200 ? resolve : reject)({responseText: text, status: status});
41+
};
42+
43+
script.onload = handler;
44+
script.onerror = handler;
45+
46+
document.body.appendChild(script);
47+
});
48+
49+
};

src/promise.js renamed to src/lib/promise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Promise polyfill (https://gist.github.com/briancavalier/814313)
2+
* Promise polyfill. (https://gist.github.com/briancavalier/814313)
33
*/
44

55
function Promise (executor) {

src/lib/util.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Utility functions.
3+
*/
4+
5+
var _ = exports;
6+
7+
_.isArray = Array.isArray;
8+
9+
_.isFunction = function (obj) {
10+
return obj && typeof obj === 'function';
11+
};
12+
13+
_.isObject = function (obj) {
14+
return obj !== null && typeof obj === 'object';
15+
};
16+
17+
_.isPlainObject = function (obj) {
18+
return Object.prototype.toString.call(obj) === '[object Object]';
19+
};
20+
21+
_.options = function (key, obj, options) {
22+
23+
var opts = obj.$options || {};
24+
25+
return _.extend({},
26+
opts[key],
27+
options
28+
);
29+
};
30+
31+
_.each = function (obj, iterator) {
32+
33+
var i, key;
34+
35+
if (typeof obj.length == 'number') {
36+
for (i = 0; i < obj.length; i++) {
37+
iterator.call(obj[i], obj[i], i);
38+
}
39+
} else if (_.isObject(obj)) {
40+
for (key in obj) {
41+
if (obj.hasOwnProperty(key)) {
42+
iterator.call(obj[key], obj[key], key);
43+
}
44+
}
45+
}
46+
47+
return obj;
48+
};
49+
50+
_.extend = function (target) {
51+
52+
var array = [], args = array.slice.call(arguments, 1), deep;
53+
54+
if (typeof target == 'boolean') {
55+
deep = target;
56+
target = args.shift();
57+
}
58+
59+
args.forEach(function (arg) {
60+
extend(target, arg, deep);
61+
});
62+
63+
return target;
64+
};
65+
66+
function extend(target, source, deep) {
67+
for (var key in source) {
68+
if (deep && (_.isPlainObject(source[key]) || _.isArray(source[key]))) {
69+
if (_.isPlainObject(source[key]) && !_.isPlainObject(target[key])) {
70+
target[key] = {};
71+
}
72+
if (_.isArray(source[key]) && !_.isArray(target[key])) {
73+
target[key] = [];
74+
}
75+
extend(target[key], source[key], deep);
76+
} else if (source[key] !== undefined) {
77+
target[key] = source[key];
78+
}
79+
}
80+
}

src/lib/xhr.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* XMLHttp request.
3+
*/
4+
5+
var _ = require('./util');
6+
var Promise = require('./promise');
7+
8+
module.exports = function (url, options) {
9+
10+
var request = new XMLHttpRequest(), promise;
11+
12+
if (_.isFunction(options.beforeSend)) {
13+
options.beforeSend.call(this, request, options);
14+
}
15+
16+
if (options.emulateHTTP && /^(put|patch|delete)$/i.test(options.method)) {
17+
options.headers['X-HTTP-Method-Override'] = options.method;
18+
options.method = 'post';
19+
}
20+
21+
if (options.emulateJSON && _.isPlainObject(options.data)) {
22+
options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
23+
options.data = url.params(options.data);
24+
}
25+
26+
if (_.isObject(options.data) && /FormData/i.test(options.data.toString())) {
27+
delete options.headers['Content-Type'];
28+
}
29+
30+
if (_.isPlainObject(options.data)) {
31+
options.data = JSON.stringify(options.data);
32+
}
33+
34+
promise = new Promise(function (resolve, reject) {
35+
36+
request.open(options.method, url(options), true);
37+
38+
_.each(options.headers, function (value, header) {
39+
request.setRequestHeader(header, value);
40+
});
41+
42+
request.onreadystatechange = function () {
43+
44+
if (this.readyState === 4) {
45+
46+
if (this.status >= 200 && this.status < 300) {
47+
resolve(this);
48+
} else {
49+
reject(this);
50+
}
51+
}
52+
};
53+
54+
request.send(options.data);
55+
});
56+
57+
_.extend(promise, {
58+
59+
abort: function () {
60+
request.abort();
61+
}
62+
63+
});
64+
65+
return promise;
66+
};

src/resource.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
module.exports = function (Vue) {
1+
/**
2+
* Service for interacting with RESTful services.
3+
*/
24

3-
var _ = require('./util')(Vue);
5+
var _ = require('./lib/util');
46

5-
/**
6-
* Resource provides interaction support with RESTful services.
7-
*/
7+
module.exports = function (Vue) {
88

9-
function Resource (url, params, actions) {
9+
function Resource(url, params, actions) {
1010

1111
var self = this, resource = {};
1212

@@ -27,7 +27,7 @@ module.exports = function (Vue) {
2727
return resource;
2828
}
2929

30-
function opts (action, args) {
30+
function opts(action, args) {
3131

3232
var options = _.extend({}, action), params = {}, data, success, error;
3333

0 commit comments

Comments
 (0)