Skip to content

Instantly share code, notes, and snippets.

@afucher
Created December 20, 2016 00:43
Show Gist options
  • Select an option

  • Save afucher/874d110f97706a075600e8da3eb74b1e to your computer and use it in GitHub Desktop.

Select an option

Save afucher/874d110f97706a075600e8da3eb74b1e to your computer and use it in GitHub Desktop.
'use strict';
import Vue from 'vue';
export default {
getCourses() {
return Vue.resource('/courses').get();
},
putCourses(){
return Vue.resource('/courses').put();
}
}
@fdaciuk

fdaciuk commented Dec 20, 2016

Copy link
Copy Markdown

Isso não seria nem relacionado ao Vue, só JS.. você poderia fazer algo do tipo:

'use strict';

import Vue from 'vue';

export default {
    resource: Vue.resource('/courses'),

    getCourses() {
        return this.resource.get();
    },
    putCourses(){
        return this.resource.put();
    }
}

@fdaciuk

fdaciuk commented Dec 20, 2016

Copy link
Copy Markdown

Ou pra não usar singleton, pode fazer algo do tipo:

'use strict';

import Vue from 'vue';

export default () => ({
    resource: Vue.resource('/courses'),

    getCourses() {
        return this.resource.get();
    },
    putCourses(){
        return this.resource.put();
    }
})

E no arquivo que você for importar esse CourseService, você faz assim:

import CourseService from './CourseService'

// e execute a função no momento em que o `Vue.resource` deveria estar disponível
const course = CourseService()

// agora você pode usar o get, put, etc
course.getCourses()
course.putCourses()

Sacou?

@afucher

afucher commented Dec 20, 2016

Copy link
Copy Markdown
Author

Valeu @fdaciuk! Juntei o que você falou com essa issue e deu certo! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment