HTTP - Axios integrated
This commit is contained in:
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 TestView from './view';
|
||||
import _ from 'underscore';
|
||||
import Http from './http';
|
||||
|
||||
Backbone.history.start();
|
||||
|
||||
class Index {
|
||||
constructor() {
|
||||
new TestView();
|
||||
Http.config = {
|
||||
baseURL: 'http://demo1393194.mockable.io/'
|
||||
};
|
||||
new TestView(); //MainView
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import Backbone from 'backbone';
|
||||
import * as _ from 'underscore';
|
||||
import _ from 'lodash';
|
||||
import $ from 'jquery';
|
||||
import Mustache from 'mustache';
|
||||
import axios from 'axios';
|
||||
import Http from './http';
|
||||
|
||||
|
||||
export default class MyView extends Backbone.View {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
@@ -13,11 +16,18 @@ export default class MyView extends Backbone.View {
|
||||
this.el = '#myarea';
|
||||
this.$el = $(this.el);
|
||||
this.template = 'Hello';
|
||||
Http.service.get('olapa')
|
||||
.then(function (response) {
|
||||
console.log(response);
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
this.render();
|
||||
|
||||
}
|
||||
|
||||
render() {
|
||||
console.log(this.el)
|
||||
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 MyView from './myview';
|
||||
|
||||
import Http from './http';
|
||||
|
||||
|
||||
export default class TestView extends Backbone.View {
|
||||
constructor() {
|
||||
@@ -13,7 +15,15 @@ export default class TestView extends Backbone.View {
|
||||
initialize() {
|
||||
this.el = 'li';
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user