[Vuejs]-I have a lot of repetition in my Vue.js code, how could I fix that?

0👍

var VueCreator = function (id, endPoint) {
    var myVue = new Vue({
        el: id,

        data: {
            videos: []
        },

        ready: function() {
            this.fetchVideos();
        },

        methods: {
            fetchVideos: function() {
                this.$http.get(endPoint, function(videos) {
                    this.videos = videos;
                });
            }
        }
    });

    return myVue;
}

//pseudo code, assumes videoList is an object that has each unique list with an id and an endpoint
var lists = [];
forEach(videoList, function(obj) {
    var tempList = new VueCreate(obj.id, obj.endPoint);
    lists.push(tempList);
})

A general rule in js (and maybe all of coding) is if you need to do something more than once, write a function to do it.

Leave a comment