Best Practice: Select by id or class with jQuery? - dom

Assuming that every element in the DOM has its own unique class name:
Is it better practice to use IDs versus class names when selecting elements in the DOM with jQuery?
Are there any performance advantages when using one over the other in jQuery?
I've been told that traditionally getting a DOM element via ID instead of by class is much faster and usually better practice, but does that apply to jQuery as well?
Many thanks in advance!

jQuery just leverages browser functionality. On older browsers(IE<9), there is no getElementsByClassName function, but virtually every browser supports getElementById. On these browsers, jQuery has to traverse the whole tree and look for all elements with the given classname. Therefore, using IDs will be faster.
However, bear in mind that id's must be unique, i.e. you can't have two elements with the same ID. This is often not advisable, since components may be used in different contexts on the same page. Using IDs exclusively would prevent that.

Selecting by ID is still faster than by class-names in jquery.

Related

Locators in Selenium Ide

I have just started to learn selenium ide and get to know that there are many locators such as ID, Name, CSS, Link, Xpath, DOM To find out the elements. What is the Best locator to find out the elements in a page?
Each locaters have pros & cons, better way to use according to your requirement. The following post will help you,
ID :- Pros - Each id is supposed to be unique so no chance of
matching several elements. Cons - Works well only on elements with
fixed ids and not generated ones
Name:- PROS: Works well with fixed list of similar elements CONS:
Difficult to use with data-bound lists.
Link:- PROS: Will only select anchor elements Useful when testing
navigation CONS: You have to know the text of the link before.
DOM:- PROS: Javascript allows you to build dynamic locators CONS:
Relies on the structure of the page
Xpath:- PROS: Allows very precise locators CONS: Slower than CSS.
Relies on browser?s XPath implementation which is not always complete
(especially on IE) and as such is not recommended for cross-browser
testing
Css:- PROS: Much faster than XPath. Widely used. Provides a good
balance between structure and attributes. Allows for selection of
elements by their surrounding context CONS: They tend to be more
complex and require a steeper learning curve

Is There An Equivalent to EnsureDebugId() for Non-Widget Elements in GWT?

I'm currently writing automated tests for a GWT application using Selenium and Java. For page elements that are Widgets, this is relatively straightforward, given that unique IDs can be set using the ensureDebugIds() method.
However, some types on the pages I work with are not widgets, but other types, possibly container types (such as Cells or grid rows that are essential HTML tables). Is there a similar method to ensureDebugIds() for this? Or is there a way to create custom IDs for these elements that acts in a similar way compatible with ensureDebugId()?
You would need to closely collaborate with you dev team to set Id's on any element which GWT does not handle in its ensureDebugId. Its straightforward to id as all UIObjects have setId api. If any HTML code is being is injected then ids need to be determined by a simple convention and set up for Selenese test case writing to be easier.

Displaying results of perform find in a portal

I have some global variables $$A, $$B, $$C and what to search within a table for these terms in fieldA, fieldB and fieldC (using Perform Find). How can I use the result of this Perform Find to display the results in a portal.
The implementation by my predecessor replaces a field fieldSEARCHwith 1 if it is in the Perform Find results and 0 otherwise, and then uses a portal filtered by this field. This seems a very dodgey way of doing it, not least becuase it means that multiple users will not be able to search at the same time!
Can you enhance the portal filter to filter against the variables themselves? Or you can perform the find, grab IDs of the found set, put them into a global field, and then use the field to construct the relationship. Global fields are multi-user safe.
The best way is not to do this at all, but use list views to perform searches. List views are naturally searchable and much more flexible than portals (you can easily sort them, omit arbitrary records, and so on). It's possible to repeat this functionality in portals, but it's way more complex. I mean, if there's some serious gain from using a portal, then it's doable, but if not, then the native way is obviously better.
List views are easier to search, as FileMaker still hasn't transitioned to the 21st century and insists on this model... Most users however want a Master-Detail view, like a mail app, and understandably so as it's more intuitive (i.e. produce a list view on one side, but clicking on it updates detail/fields in the middle).
If this is what you want, you may want to cast an eye at Modular FM, where someone has already done the hard work for you:
http://www.modularfilemaker.org/module/masterdetail-2-0/
HTH
Stam

Recommendations on structure for Mongoid/MongoDB Tree of Tags

I'm looking for some recommendations on how to structure the tags part of this data model:
Here's a simplified version of it:
a Site has many Posts (relational association [references_many in mongoid speak]). A Site has a tree of tags
a Post has an array of tags (subset of the Site's tags, order doesn't matter)
The use cases I care about are:
Quickly saving & retrieving the Site's tags in tree form (ie to be able to display them as a tree in the UI)
Quickly querying which of a Site's posts have a certain tag.
Without the tree structure, http://github.com/wilkerlucio/mongoid_taggable solves my usecases. I've seen some of the acts_as_tree ports for Mongoid like:
http://github.com/benedikt/mongoid-tree
http://github.com/saks/mongoid_acts_as_tree
http://github.com/ticktricktrack/mongoid_tree
They all seem to take a relational approach, as opposed to embedded, to storing the hierarchy, which would mean that both of the use cases above would be slow (likely requiring a map/reduce).
Has anyone done anything similar, or have any advice? Ideally I'd love a Mongoid solution, but I'm happy to drop down to the Ruby driver as well.
Do you need to update the structure of the tree (i.e. move a tag to another parent)? If that is possible, the embedded approach would become difficult, and the relational/normalized approach makes more sense.
I would probably store the tags themselves in the document (embedded), but if there is any chance that I need to move tree nodes around on-line, then I'd store the hierarchy in another document. Queries need not be slow, if you first flatten the search query (according to the current tree) and then search for those tags. This approach probably does not scale to well if the flattened search query ends up having hundreds of tags in them (how tall is your tree?).
If tags cannot be moved to new parents (or only by you, during scheduled maintenance), go ahead and embed the whole hierarchy.
there are two implemented patterns of mongodb tree structure

Options for handling a frequently changing data form

What are some possible designs to deal with frequently changing data forms?
I have a basic CRUD web application where the main data entry form changes yearly. So each record should be tied to a specific version of the form. This requirement is kind of new, so the existing application was not built with this in mind.
I'm looking for different ways of handling this, hoping to avoid future technical debt. Here are some options I've come up with:
Create a new object, UI and set of tables for each version. This is obviously the most naive approach.
Keep adding all the fields to the same object and DB tables, but show/hide them based on the form version. This will become a mess after a few changes.
Build form definitions, then dynamically build the UI and store the data as some dictionary like format (e.g. JSON/XML or maybe an document oriented database) I think this is going to be too complex for the scope of this app, especially for the UI.
What other possibilities are there? Does anyone have experience doing this? I'm looking for some design patterns to help deal with the complexity.
First, I will speak to your solutions above and then I will give my answer.
Creating a new table for each
version is going to require new
programming every year since you will
not be able to dynamically join to
the new table and include the new
columns easily. That seems pretty obvious and really makes this a bad choice.
The issues you mentioned with adding
the columns to the same form are
correct. Also, whatever database you
are using has a max on how many
columns it can handle and how many
bytes it can have in a row. That could become another concern.
The third option I think is the
closest to what you want. I would
not store the new column data in a
JSON/XML unless it is for duplication
to increase speed. I think this is
your best option
The only option you didn't mention
was storing all of the data in 1
database field and using XML to
parse. This option would make it
tough to query and write reports
against.
If I had to do this:
The first table would have the
columns ID (seeded), Name,
InputType, CreateDate,
ExpirationDate, and CssClass. I
would call it tbInputs.
The second table would have the have
5 columns, ID, Input_ID (with FK to
tbInputs.ID), Entry_ID (with FK to
the main/original table) value, and
CreateDate. The FK to the
main/original table would allow you
to find what items were attached to
what form entry. I would call this
table tbInputValues.
If you don't
plan on having that base table then
I would use a simply table that tracks the creation date, creator ID,
and the form_id.
Once you have those you will just need to create a dynamic form that pulls back all of the inputs that are currently active and display them. I would put all of the dynamic controls inside of some kind of container like a <div> since it will allow you to loop through them without knowing the name of every element. Then insert into tbInputValues the ID of the input and its value.
Create a form to add or remove an
input. This would mean you would
not have much if any maintenance
work to do each year.
I think this solution may not seem like the most eloquent but if executed correctly I do think it is your most flexible solution that requires the least amount of technical debt.
I think the third approach (XML) is the most flexible. A simple XML structure is generated very fast and can be easily versioned and validated against an XSD.
You'd have a table holding the XML in one column and the year/version this xml applies to.
Generating UI code based on the schema is basically a bad idea. If you do not require extensive validation, you can opt for a simple editable table.
If you need a custom form every year, I'd look at it as kind of a job guarantee :-) It's important to make the versioning mechanism and extension transparent and explicit though.
For this particular app, we decided to deal with the problem as if there was one form that continuously grows. Due to the nature of the form this seemed more natural than more explicit separation. We will have a mapping of year->field for parts of the application that do need to know which data is for which year.
For the UI, we will be creating a new page for each year's form. Dynamic form creation is far too complex in this situation.