How should I build a component that has emits defined from props. I Implemented a solution and I think there should be a better way. #10132
Unanswered
RamonDonadeu
asked this question in
Help/Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello! This will need a large explanation, I think, so... let's begin.
I'm building a web App with for my company, and now we will start other apps, so I'm trying to move some components to a NPM package so it is shared between different projects.
I'm using
q-table
component from QuasarThe app has multiple pages where a table is displayed with thousands of lines and each table has different actions to perform to each line.
That component is called "CommonTable" it has a prop called "pageActions" Where I define multiple parameters, the
action type:
Selection
And it has more parameters, here is a
pageActions
configuration file example:Usually, when a selection is needed a function must be emitted from that component to its parent, as each table can have 10 different functions and there are multiple ways to call one of the actions, from right-clicking each row, from a TableTopBar, From a Sidebar, when a value of a row is modified... And maybe I have 15 different pages with its own table. This tables, are a very complex component divided in multiple files, but probably they share about 2000 lines of code ( Including comments )
I am a Junior Developer with a Software Engineering Degree, 3 years of work experience, 1 year on Vue and 1 in React, and I work 'alone' in this project, maybe you could say that I'm underqualified for this job, but honestly I don't have any problem developing this project, I'm not really alone as there are more people in the team but not experienced in Vue development, although they know some things about frontend that I can ask them.
But this problem never had a solution we all liked, so I implemented is emitting a common function called 'CallFunction'
Let's say that an action named
printRow
is called from a row in the table, the row component emits:emit('callFunction', {functionName: 'printRow', parameters: {parameter1: 1, ... }
Then its parent
commonTable
receives this emit and emits the same thingAnd then the main page that implements the
commonTable
component, receives the emitEach Page that has a table, has this function called
callFunction
where it gets the function name and call it from an object where all the available functions are called.I think this is not an elegant and correct solution as I need to duplicate this structure in each page, but the other way would be having all the hundreds of functions available as emits in the
CommonTable
component, and each emit will be used only once. So I don't like that solution either. Maybe I should create multiple Common Tables, each with the commits of the page they are in. But I would need also the 'callFunction' emits in theCommonTable
childs and when arriving toCommonTable
I should get the function name andemit(props.functionName)
As I'm relatively new, and I don't have much architecture experience, I decided that this was the best decision for not creating duplicate components. But I'm open to hearing other better solutions.
Beta Was this translation helpful? Give feedback.
All reactions