The application I’m currently working on has a Glossary model. The
Glossary table has columns for the term and its definition.
The application also has Pages which have content. The content needs to
highlight any words that match a term in the Glossary table.
There’ll be a few ways to do this but I’d appreciate your advice on the
way you’d accomplish this.
Yes, there are a lot of ways to do this. My approach would be:
Send the names of all the Glossary as json object when you load the page, bcoz you will be matching the contents of pages with names, i guess.
You could send the names as a string like " name1 | name2 | name3 | …| " or simply pass a hash.
Now, when you display the contents of a page, use jQuery to do the matching on the names json with the contents in the http_response coming from server.
How you want to do the matching would determine, in which format you are sending the names json from server.
Advantages of this approach:
You are not saving the matches in DB, although you can. But if you change your glossary, you would have to do it again and change the matches for each page’s contents. You would have understood what I mean to convey here.
Use javascript libraries when it comes to things like these, until and unless you want to save the changes(matches here) in DB.
There could be a better way of doing this, so lets wait and see what the experienced ones here say about it.
The application I’m currently working on has a Glossary model. The
Glossary table has columns for the term and its definition.
The application also has Pages which have content. The content needs to
highlight any words that match a term in the Glossary table.
Why are you using the server to do this, when you could use client processor to do the matching for you.
NOTE:
He just wants to highlight them and not show them as links.
If it had been links, I would have completely agreed with your approach.
The application I’m currently working on has a Glossary model. The
Glossary table has columns for the term and its definition.
The application also has Pages which have content. The content needs to
highlight any words that match a term in the Glossary table.
Why are you using the server to do this, when you could use client processor to do the matching for you.
NOTE:
He just wants to highlight them and not show them as links.
If it had been links, I would have completely agreed with your approach.
I’m assuming that he wants to link them because I think there’s no use just highlighting them.
itself but a glossary listing will also be available.
Honestly, I’m not really sure what the best method of displaying the
definition on the page would be; I don’t want the definition to be
obtrusive.
Where do you want to show the definition, and on what event, i mean is it
like when you hover over the highlighted text or what?
onmouseover
When you are doing the matching using jQuery, you could change the content of pages, in such a way that each matching word (highlighted word) is like an anchor tag or simple text, which when hovered over, can do two things via jQuery:
Fire an ajax call to the server, asking for the description of the name.
or
Send the description alongwith the names in json format, and when hovered over, display the description from the json object you have.
itself but a glossary listing will also be available.
Honestly, I’m not really sure what the best method of displaying the
definition on the page would be; I don’t want the definition to be
obtrusive.
Where do you want to show the definition, and on what event, i mean is it
like when you hover over the highlighted text or what?
onmouseover
When you are doing the matching using jQuery, you could change the content of pages, in such a way that each matching word (highlighted word) is like an anchor tag or simple text, which when hovered over, can do two things via jQuery:
Fire an ajax call to the server, asking for the description of the name.
I think this is the better option. plus you can minimize the request to the server if you somehow store already hovered words into a global js variable.
so onmouseover, check the array if the word is there. if it’s there, then you don’t need to do an ajax request anymore.