Refactoring vue.js
This commit is contained in:
parent
7ed350445f
commit
e2e6ba7075
@ -15,7 +15,7 @@
|
||||
<div class="jumbotron">
|
||||
<h1>Bouquins</h1>
|
||||
<p>Cette bibliothèque contient actuellement <strong>{{ booksCount }}</strong> livres et BD en format papier ou électronique.</p>
|
||||
<button class="btn btn-primary" type="button" v-on:click="loadBooks">Livres</button>
|
||||
<button class="btn btn-primary" type="button" @click="loadBooks">Livres</button>
|
||||
</div>
|
||||
<div class="table-responsive" v-if="books.length > 0">
|
||||
<table class="table table-striped">
|
||||
|
58
index.js
58
index.js
@ -1,8 +1,11 @@
|
||||
(function(root) {
|
||||
'use strict';
|
||||
|
||||
function sendQuery(url, error, success)
|
||||
{
|
||||
var app = new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
books: [],
|
||||
booksCount: 0
|
||||
},
|
||||
methods: {
|
||||
sendQuery: function(url, error, success) {
|
||||
var xmh = new XMLHttpRequest();
|
||||
var v;
|
||||
|
||||
@ -26,39 +29,24 @@
|
||||
|
||||
xmh.open('GET', url, true);
|
||||
xmh.send(null);
|
||||
}
|
||||
|
||||
function stdError(code, resp) {
|
||||
},
|
||||
stdError: function(code, resp) {
|
||||
console.log('ERROR ' + code + ': ' + resp);
|
||||
},
|
||||
indexSuccess: function(resp) {
|
||||
this.booksCount = resp.count;
|
||||
},
|
||||
loadIndex: function() {
|
||||
this.sendQuery('cgi-bin/bouquins/index', this.stdError, this.indexSuccess);
|
||||
},
|
||||
booksSuccess: function(resp) {
|
||||
this.books = resp;
|
||||
},
|
||||
loadBooks: function() {
|
||||
this.sendQuery('cgi-bin/bouquins/books', this.stdError, this.booksSuccess);
|
||||
}
|
||||
|
||||
function booksSuccess(resp) {
|
||||
app.books = resp;
|
||||
}
|
||||
|
||||
function indexSuccess(resp) {
|
||||
app.booksCount = resp.count;
|
||||
}
|
||||
|
||||
function loadIndex() {
|
||||
sendQuery('cgi-bin/bouquins/index', stdError, indexSuccess);
|
||||
}
|
||||
|
||||
function loadBooks() {
|
||||
sendQuery('cgi-bin/bouquins/books', stdError, booksSuccess);
|
||||
}
|
||||
|
||||
root.loadIndex = loadIndex;
|
||||
root.loadBooks = loadBooks;
|
||||
})(this);
|
||||
|
||||
var app = new Vue({
|
||||
el: '#app',
|
||||
data: {
|
||||
books: [],
|
||||
booksCount: 0
|
||||
},
|
||||
mounted: function() {
|
||||
loadIndex();
|
||||
this.loadIndex();
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user