HTTP - Axios integrated

This commit is contained in:
Indrajith K L
2017-11-02 15:47:14 +05:30
parent 70a7e6c3c2
commit a7ccf2688f
5 changed files with 46 additions and 4 deletions

View File

@@ -27,8 +27,11 @@
"webpack-dev-server": "^2.9.3"
},
"dependencies": {
"axios": "^0.17.0",
"backbone": "^1.3.3",
"html-loader": "^0.5.1",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"mustache": "^2.3.0",
"underscore": "^1.8.3"
}

14
src/http.js Normal file
View 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();

View File

@@ -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
}
}

View File

@@ -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));
}

View File

@@ -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();
}