This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!

Tony Singh

+ Follow
since Nov 30, 2018
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tony Singh

I have a UI where I show a list of features in a dropwdown. Based on user selection from the dropdown, I render the feature. On landing I keep that dropdown disabled until I receive some data from ajax call.
So this is the flow -
* User lands on the page, dropdown for selecting features is disabled.
* Once loading completes, I send an ajax call to receive the data.
* Once that data is obtained, I enable the dropdown.
* User then selects a feature from the dropdown.
* That feature is rendered.
* At any time user can select any feature from the dropdown.
* Based on some user interaction, I will need to fire the ajax call again & disable the dropdown until then, so that I recalculate again on the server side which all features to be shown inside the dropdown.
* Cycle continues.

Why am I waiting for data to enable the dropdown?
Because the feature list which is being shown requires some data for getting rendered. That data is received using ajax call which I trigger after landing. The data is a map where key is featureName & value is the object which will have necessary information to render the corresponding feature. Hence, I don't want to enable dropdown until I have all the data for the features to be rendered.

I tried implementing this but it's getting very messy.
Breaking down the components which I am trying to implement in a clean way -
* Store each feature's corresponding data which I am getting through ajax call.
* On receiving the data, enable the dropdown.
* When another ajax call is required, disable the dropdown.
* When user selects 1 feature, do some processing on that feature's corresponding data & render that feature.

I am thinking about flux pattern where my first action would be when data is received from the ajax call & after that the further actions would come from UI depending on which feature user has asked to render. This approach is also not looking good.
Can there be any cleaner approach/design pattern which I can follow in Javascript. Any suggestions will be greatly appreciated.
4 weeks ago
Let’s assume a UI where currently we show students details on tiles. One tile per student. Now we need to refactor our code to support a new requirement. The new requirement is to change the order of those tiles based on different kind of sorting like – sort by student’s names, sort by student’s grades, sort by their age etc. Here, the sorting criteria will be selected by user on the run time through UI & user should be able to change the sorting criteria whenever he requires.  
Right now, we have a list of objects containing student data which contains the required information to show on the tiles. We don’t put any logic on changing the order of tiles & show them in the same order we get it from some other service. To change the order of tiles, I would need to change the order of those objects accordingly because we render tiles on the basis of order of those objects.
Summarizing the requirements –
• User should be able to sort on the basis of different criteria on the run time. Several sorting criteria will be shown to the user.
• User should be able to switch to any sorting criteria or go back to unsorted state at any time.
• Sorting criteria can be many in future. There is no limit to that.
• All this to be implemented in JS.
I am considering this optimization as well–
• Since user can go to any state on his will, I don’t want to call our sorting framework when user is selecting same sorting criteria multiple times. It should be called for the first time only & later if he comes back to same sorting criteria, we shouldn’t be calling our framework again for giving us the sorted order. Instead we should store it somewhere on the first time & keep on using it later on.  Eg. - Let say we rendered the tiles in unsorted order. User selects “sort by age” option. We call our framework & show the reordered tiles on the basis of its output. Now user selects some other sort criteria let say “sort by grade” & we call our framework again & reorder tiles based on that. Now user again selects “sort by age” option. Now we shouldn’t be calling our sort framework to sort on the basis of this criteria again to give us the results. Instead we should have saved this data somewhere in the first place so that it can be reused.

Sorting framework is also being created at the same time & is owned by us only. So, we have flexibility to decide the type of inputs & outputs to the framework. However regarding the sorting logic & design, let’s consider it a black box since there I have already decided to go with decorator pattern.
But looks like code changes on the feature side is going to be messy since possibilities for new sorting requirements is endless & we have to maintain the object states of each for reusability. I am brainstorming on several questions like
• How & Where to store the “sort criteria” to its corresponding data which is going to be the input of the sorting framework. Eg. – Let say I want to sort on the basis of age. I would need to get this data & make a map of “sorting criteria to age” for each student. So, I would create a “Map<Student, Map <SortCritria, respectiveData> > This is straightforward approach but can there be any cleaner approach since sort criteria can be many. Also, the big question is where should this logic be placed
• What should be the output of sorting framework. Should it return <oldIndex to newIndex> map or  <uniqueStudentId to newIndex > map or anything else.
• Should I introduce a new layer between (eg. Store) our feature & sorting framework or
• should object states be stored in framework or in the feature code base. Etc.
I am looking for a clean approach or a design pattern where all these requirements can fit in. Any suggestions would be greatly appreciated.
1 month ago
Thanks Campbell for your kind help. I have done some research on both the suggested patterns & looks like Decorator pattern will fit in my requirements.
1 month ago
Requirements -
* Sort/segregate students on the basis of various criterias.
* Right now, we will be sorting on the basis of students age but in future different kind of sorting requirements may come.
* Future requirements can be any like sort students on the basis of their father’s age, segregate students on the basis of grades they got etc.
* For all the sorting/segregation types, list of students objects will be provided.
* Create a reusable framework which can be used for age based sorting & future sorting requirements which may come.
1 month ago