HTTP - Axios integrated
This commit is contained in:
@@ -27,8 +27,11 @@
|
|||||||
"webpack-dev-server": "^2.9.3"
|
"webpack-dev-server": "^2.9.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^0.17.0",
|
||||||
"backbone": "^1.3.3",
|
"backbone": "^1.3.3",
|
||||||
|
"html-loader": "^0.5.1",
|
||||||
"jquery": "^3.2.1",
|
"jquery": "^3.2.1",
|
||||||
|
"lodash": "^4.17.4",
|
||||||
"mustache": "^2.3.0",
|
"mustache": "^2.3.0",
|
||||||
"underscore": "^1.8.3"
|
"underscore": "^1.8.3"
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/http.js
Normal file
14
src/http.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
class Http {
|
||||||
|
constructor() {
|
||||||
|
this.service = axios.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
set config(configOptions) {
|
||||||
|
this.service = axios.create(configOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default new Http();
|
||||||
@@ -1,12 +1,17 @@
|
|||||||
import Backbone from 'backbone';
|
import Backbone from 'backbone';
|
||||||
import TestView from './view';
|
import TestView from './view';
|
||||||
import _ from 'underscore';
|
import _ from 'underscore';
|
||||||
|
import Http from './http';
|
||||||
|
|
||||||
Backbone.history.start();
|
Backbone.history.start();
|
||||||
|
|
||||||
class Index {
|
class Index {
|
||||||
constructor() {
|
constructor() {
|
||||||
new TestView();
|
Http.config = {
|
||||||
|
baseURL: 'http://demo1393194.mockable.io/'
|
||||||
|
};
|
||||||
|
new TestView(); //MainView
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import Backbone from 'backbone';
|
import Backbone from 'backbone';
|
||||||
import * as _ from 'underscore';
|
import _ from 'lodash';
|
||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
import Mustache from 'mustache';
|
import Mustache from 'mustache';
|
||||||
|
import axios from 'axios';
|
||||||
|
import Http from './http';
|
||||||
|
|
||||||
|
|
||||||
export default class MyView extends Backbone.View {
|
export default class MyView extends Backbone.View {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
@@ -13,11 +16,18 @@ export default class MyView extends Backbone.View {
|
|||||||
this.el = '#myarea';
|
this.el = '#myarea';
|
||||||
this.$el = $(this.el);
|
this.$el = $(this.el);
|
||||||
this.template = 'Hello';
|
this.template = 'Hello';
|
||||||
|
Http.service.get('olapa')
|
||||||
|
.then(function (response) {
|
||||||
|
console.log(response);
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
this.render();
|
this.render();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
console.log(this.el)
|
|
||||||
this.$el.html(Mustache.render(this.template));
|
this.$el.html(Mustache.render(this.template));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
12
src/view.js
12
src/view.js
@@ -4,6 +4,8 @@ import $ from 'jquery';
|
|||||||
import Mustache from 'mustache';
|
import Mustache from 'mustache';
|
||||||
import MyView from './myview';
|
import MyView from './myview';
|
||||||
|
|
||||||
|
import Http from './http';
|
||||||
|
|
||||||
|
|
||||||
export default class TestView extends Backbone.View {
|
export default class TestView extends Backbone.View {
|
||||||
constructor() {
|
constructor() {
|
||||||
@@ -13,7 +15,15 @@ export default class TestView extends Backbone.View {
|
|||||||
initialize() {
|
initialize() {
|
||||||
this.el = 'li';
|
this.el = 'li';
|
||||||
this.$el = $(this.el);
|
this.$el = $(this.el);
|
||||||
this.template = '<div id="myarea"></div>'
|
this.template = '<div id="myarea"></div>';
|
||||||
|
|
||||||
|
Http.service.get('test1').then(function (response) {
|
||||||
|
console.log(response);
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
this.render();
|
this.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user