vue2/examples/commits/app.js

57 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-12-12 06:44:25 +08:00
/* global Vue */
2016-04-16 04:20:35 +08:00
var apiURL = 'https://api.github.com/repos/vuejs/vue/commits?per_page=3&sha='
/**
* Actual demo
*/
2018-12-12 06:44:25 +08:00
new Vue({
2016-04-16 04:20:35 +08:00
el: '#demo',
data: {
branches: ['master', 'dev'],
currentBranch: 'master',
commits: null
},
created: function () {
this.fetchData()
},
watch: {
currentBranch: 'fetchData'
},
2016-04-30 07:40:53 +08:00
filters: {
2016-04-16 04:20:35 +08:00
truncate: function (v) {
var newline = v.indexOf('\n')
return newline > 0 ? v.slice(0, newline) : v
},
formatDate: function (v) {
return v.replace(/T|Z/g, ' ')
2016-04-30 07:40:53 +08:00
}
},
methods: {
2016-04-16 04:20:35 +08:00
fetchData: function () {
var self = this
2018-12-12 06:44:25 +08:00
if (navigator.userAgent.indexOf('PhantomJS') > -1) {
// use mocks in e2e to avoid dependency on network / authentication
setTimeout(function () {
self.commits = window.MOCKS[self.currentBranch]
}, 0)
} else {
var xhr = new XMLHttpRequest()
xhr.open('GET', apiURL + self.currentBranch)
xhr.onload = function () {
self.commits = JSON.parse(xhr.responseText)
console.log(self.commits[0].html_url)
}
xhr.send()
2016-04-16 04:20:35 +08:00
}
}
}
})