Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maxxxir's avatar

need feedback from front developer for an idea , centralizing backend resources/api calls in a single class

hi im a backend developer , recently i've started working with vue for creating some small fullstack apps

one thing that i found very annoying was loading the resources/entities from backend and passing them to the components via props or directly in the component

so my idea was to register each component and its needed resources in a single class (resourceManager) , let the resource manager call the backend api and add the resources to the components after they are available

this way my components will be much cleaner , there wont be any duplicate backend calls and i dont have to worry about components loading at any point of app lifecycle

here is a simple example , i have 2 components Users.vue and Tasks.vue and they both need the users list from backend (please ignore any typo or syntax errors )

Users.vue

export default {
    data() {
        return {
            users: [],
            someLocalvar : false 
        }
    },
    created() {
        this.resourceManager.registerComponent(this, [
            {resource: 'users', route: {name: 'api.users'}},
        ])
    }
}

Tasks.vue

export default {
    data() {
        return {
            users: [],
            tasks: [],
        }
    },
    created() {
        this.resourceManager.registerComponent(this, [
            {resource: 'users', route: {name: 'api.users'}},
            {resource: 'tasks', route: {name: 'api.tasks'}},
        ])
    }
}

here is a simplified version of my resourceManager.js


class resourceManager {

    constructor () {
        this.resources = {}
        this.components = {};
    }

    registerComponent(component, resources ){

        let component_key =   component.$options.__file?.split('resources/') [1]|| component.$.uid;
        this.components[component_key] = {component , resources} ;

        resources.forEach((item) => this.callApi(item.route) )

        this.seedMountedComponent(component)
    }


    async callApi(route  ){

        if( this.resources[route])
        {
           // already called by another component , resource is in queue or available 
            return ;
        }

        // cll api and get the data && and to this.resources
        this.resources[route] = axioscall(route).data;

        this.resourceIsAvilable(route);
    }

    resourceIsAvilable(availableResourceRoute){
        // loop this.components and add the available resource to them if they have it
              for (const componentName in this.components) {
            this.components[componentName].resources.forEach( (componentResource) => {
                if(availableResourceRoute=== componentResource.route.name )
                {
                    this.components[componentName].component[componentResource.resource] = this.resources[availableResourceRoute]
                }
            })
        }

    }


    seedMountedComponent(component){
        // loop available resources and add them to created component
    }
}

this has made my work so much easier and cleaner , but not being a front developer im not sure if there a is problem with this approach that i dont see or maybe there is already a simple out of the box solution that does that

ofcourse there is more to do like if one component makes some changes to backend data it t should be updated in the resourcemanger and added again to the components ... i will handle those scenarios as well

i appreciate any feed back from someone with frontend experience

0 likes
2 replies
hajrovica's avatar

Hi i am currently more in Angular ecosystem than vue, and it is great if this simplified workflow for you but some observations:

PROS:

  • resource management centralization
  • code reusability

CONS:

  • another abstraction increasing complexity
  • stale data issue - which you have laready recognized
  • API calls tight coupling
  • synchrounous API calls (calls will block event loop which will lead to prerformance degradation)

So for me this is a bit of 50/50 issue, it reminds me to service and store patterns in Angular, stale data can become issue and very fast but that heavily depends on application logic and number of componentes.

On the other hand if only small applications are in in play go for it.

But if apps start to grow and You see/recognize issues with data consistency and speed that would be good point to take some other approach into consderation, like store pattern ?

1 like
maxxxir's avatar

@hajrovica thanx for the feedback

stale data issue is easy fix , i can use event bus to signal to resource manager every time changes made so it would get the updated resource from backend or event better maybe even move the change request to resource manager as well so my components dont have to make any api call at all

	function updateResource( updateRouteAndpayload , resourceRoute )
	{
	   // await ajax call for update/delete/create backend ; 
	   this.callApi({resourceRoute , true  });
	}
	
    async callApi(route , overwrite = false   ){

        if( this.resources[route] && !overwrite)
        {
           // already called by another component , resource is in queue or available 
            return ;
        }

        // cll api and get the data && and to this.resources
        this.resources[route] = axioscall(route).data;
        this.resourceIsAvilable(route);
    }

my goal is to remove any backend request from components all together and move this responsibility to resourcemanager so i guess tight coupling is ok

yes the api calls are synchronous , but im not blocking the app when i call async method so the rest of the app can function as if there is no data / is empty (basically im not using await when i call async method )

again im not a js developer so maybe i dont understand the issu


	function entrypint( component ){
		blockingMethod();
	   // do other stuff 
	}


	async function blockingMethod(){

	   // do blocking tasks 
	   // attach to component when it's done 

	}

i think do other stuff will happen without being blocked if i call the method without await ? also api calls are already happening in the component anyway so i guess it makes no difference , same process just in a different different file/class ?

thanx again for the feedback

Please or to participate in this conversation.