Q.Core

This is the heart of most Q features. On its own, Q.Core is an impressive set of methods and features, though only when coupled with other parts of the Q Library, it really shines. Q.Core is the groundwork, the base layer for all the advanced features of Q, including:

Q.Core lives in its own namespace, Q.*, or Window.Q.*, if you want to be precise. Plugins created and registered via Q become proper jQuery plugins, accessible in all the standard ways. To find out more about this, make sure to read the Q.PluginFactory documentation.

Tip: Do Not Be Afraid Of HTML5

There's been a lot said and written about HTML5. Most browsers support at least part of the specification, yet many developers are reluctant to use it. Adoption of at least several HTML5 features will allow you to leverage your Q experience and make your "rapid development" truly that.

Q.Core in your development

If you want to use other features of Q, Q.Core is a must for you. Without it, the whole library simply won't work. But you don't need to limit yourself only to using it for Q related stuff. Q.Core provides features and methods that are Q-agnostic and can also be employed independently.

Q.Core is written as a static object, meaning that you don't need to run any initialization before using its features. All of its methods are available immediately after the library has been loaded.

To start using Q.Core, simply place the Q Library somewhere in your web application's folders, and refer to Q.Core using the <script> HTML tag:

<script type="text/javascript" src="path_to_file/q.core.js"></script>

Using Q.Core effectively

There isn't anything arcane about using Q.Core. It consists of straightforward methods that you call in an equally straightforward way. All methods have a return value; some return results of their operation (e.g. Q.GUID() or Q.HTML.decode()), others return reference to Q Library itself (e.g. Q.eventTrigger() or Q.dropEvent()).

This ensures that you can chain function calls wherever it makes sense to, and expect some kind of meaningful response from the rest.

Q.Core Properties

For your comfort, Q.Core offers a few predefined properties that you can change at any point.

Q.options

Basic configuration options for Q are available under Q.options.

language (String) Q.options.language

Sets the language for Q string variables. Currently not used, except for Q.UXTimeParse().

loggingMinimumPriority (String) Q.options.loggingMinimumPriority

While using custom logging of events and actions is a great way to improve development, it may be a major memory hog. In order to limit memory usage, Q introduces this configuration property.

Each log request contains a priority, default 0. Before Q actually logs the record, it checks whether its priority equals to or exceeds loggingMinimumPriority value.

Most log requests come with value 0. By setting Q.loggingMinimumPriority to 1, you'll prevent most records from being logged. Note that errors come with priority 1, which means they would be recorded.

Q.Core Methods

These are the methods that help Q become the ultimate tool for your rapid development needs.

AddStack (Integer) Q.AddStack( {message} )

  Returns an Integer, which is the length of current Stack.

message
This is a mandatory parameter in the form of JS Object. This object should have the following properties:
  • message: String
    Text of the message to be displayed in stacktrace records.
  • type: (optional) String
    This should be either "info", "warning", or "error" (case-insensitive). Value of this parameter is one of the filterable properties for GetStack().
    Default "info" when empty/null.
  • priority: (optional) Integer
    Priority determines whether or not the message will be recorded, based on current setting of Q.options.loggingMinimumPriority. In order to minimize memory usage and unnecessary overhead, Q usually records only stack messages of priority 1 and above.
    Default 0 when empty/null.
  • origin: (optional) String
    Due to lack of backtracking of function calls in Javascript, Q uses this optional parameter to identify the origin of the stack record. Recommended value is, for example, "myJSClass.myFunction()".
    Default false when empty/null.
  • arguments: (optional) Array
    Sometimes it is useful to know what arguments your function has received. By simply passing the reserved word arguments to this property, you will record an array of arguments exactly the way your function had received them.
    Default null.
  • element: (optional) jQuery DOM Object
    When appropriate, you may also want to provide a reference to a DOM element for easier debugging. This property is where you should place it.
    Default false when empty/null.
  • instance: (optional) String
    When appropriate, you may also want to provide a string identifier of the instance which recorded the Stack item. Default null.
  1. function HelloWorld()
  2. {
  3. this.id = 'HelloWorld';
  4. Q.AddStack(
  5. {
  6. message:'Hello Stacktrace!',
  7. type:'info',
  8. priority:1,
  9. origin:'HelloWorld()',
  10. arguments:arguments,
  11. element:$(document)
  12. instance:this.id
  13. }
  14. );
  15. }

Instead of a whole Object, you may also provide only a String containing the text of the message to be recorded. If you do that, the optional properties will be set to their defaults.

  1. function HelloWorld()
  2. {
  3. Q.AddStack('Hello Stacktrace!');
  4. }
GetStack (Array) Q.GetStack() (Array) Q.GetStack( {filter} )

  Returns an Array of all records in the Stack, matching the filter criteria (if any were specified).

filter
This is an optional parameter in the form of JS Object. This object may have the following properties:
  • origin: String
    If value of the origin property matches or is a substring of an origin property of a Stack record, the record will be displayed.
  • type: String
    If value of the type property matches that of a type property of a Stack record, the record will be displayed.
  • message: String
    If value of the message property matches or is a substring of a message property of a Stack record, the record will be displayed.
  • instance: String
    If value of the instance property matches or is a substring of an instance property of a Stack record, the record will be displayed.
  1. function HelloWorld()
  2. {
  3. Q.GetStack(
  4. {
  5. origin:'HelloWorld()',
  6. type:'info',
  7. message:'Hello',
  8. instance:'HelloWorld'
  9. }
  10. );
  11. }
  1. function HelloWorld()
  2. {
  3. Q.GetStack({type:'error'});
  4. }
eventTrigger (Q instance) Q.eventTrigger( "eventName", {params} )

  Returns an instance of Q.

This function allows you to trigger an existing event within your plugin. It is one of the key components to a fully customizable event model.

eventName
This String parameter is the name of an existing event. If a non-existant event is triggered, the method will create an error report in the Stack. Both successful and unsuccessful event trigger return instance of Q.
params
This optional Object parameter is passed to all event handlers as parameter. It is recommended that one of the properties of this object is instance with value this, so that the event handler can properly address the instance which triggered it:
  1. this.eventTrigger("MyEvent",{instance:this});
  1. this.onMyEvent(
  2. function(e)
  3. {
  4. e.instance.myFunction();
  5. }
  6. );

Note that plugins created using Q.PluginFactory also use event methods, though they're not using the ones from Q.Core. Therefore you would call this.eventTrigger within a plugin, instead of Q.eventTrigger.

dropEvent (Q instance) Q.dropEvent( "eventName") (Q instance) Q.dropEvent( "eventName", (eventHandler) )

  Returns an instance of Q.

This function removes either a single event handler from the queue, or drops the entire queue of event handlers.

eventName
This String parameter is the name of an existing event. If a non-existant event is referenced, the method will create an error report in the Stack. Both successful and unsuccessful function calls return instance of Q.
eventHandler
This optional Function parameter is used to identify the exact handler to be dropped. Because event handlers are most often written as closures, this is the most reliable way of identifying a handler. Q looks at all handlers registered in the event queue for that particular event, and compares their string representation to string representation of this parameter. All matching event handlers are then removed from the queue. Yes, all of them - it is not unheard of to have an event handler registered multiple times; this takes care of that issue.
Note: If you leave this parameter out, the entire event handler queue will be cleared.
  1. this.dropEvent("MyEvent");
  1. this.dropEvent(
  2. "MyEvent",
  3. function()
  4. {
  5. alert('Hello World!');
  6. }
  7. );

Note that plugins created using Q.PluginFactory also use event methods, though they're not using the ones from Q.Core. Therefore you would call this.dropEvent within a plugin, instead of Q.dropEvent.

HTML (String) Q.HTML.encode( "html" ) (String) Q.HTML.decode( "text" )

  Returns String

This Object encapsulates two key methods for handling HTML Strings: Encode and Decode. While many implementations strive to process strings using regular expressions, Q uses a temporary DOM node to perform the transformation in a fast and reliable way.

html
This String parameter contains unescaped HTML as can be, for example, retrieved with jQuery function $('#MyDOM').html().
text
This String parameter contains encoded HTML, for example a result of HTML.encode().
  1. var EncodedHTML = Q.HTML.encode( $("#MyDOM").html() );
  1. $("#MyDOM").html( Q.HTML.decode( EncodedHTML ) );
Base64 (String) Q.Base64.encode( "source" ) (String) Q.Base64.decode( "base64" )

  Returns String

This Object encapsulates two key methods for handling Base64: Encode and Decode. It is quite rare to encode and decode Base64 in Javascript, but since Q supports JSON and fully AJAX-driven interfaces, this feature will eventually be used.

source
This String parameter contains raw source data to be encoded.
base64
This String parameter contains encoded Base64 data.
  1. var BASE64EncodedString = Q.Base64.encode( "MyDataString" );
  1. var MyDataString = Q.Base64.decode( "BASE64EncodedString" );
Template (String) Q.Template( "templateId" , {data} , (filter) )

  Returns String

This function provides a way to use parts of HTML as templates for rendering data. To achieve this, the HTML code needs to be wrapped in HTML tag <template>, which is a part of HTML5 specification. Note that <template> is not rendered by browser, and therefore its contents are hidden from users, yet still available to Q.Template.

A <template> to be used by Q.Template needs to have two attributes assigned: id, and data-content. The id has to be unique in the page scope, and the data-content attribute needs to have value "QTemplate". That is how Q recognizes <template> that is assigned for its use.

  1. <template id="myTemplate" data-content="QTemplate">
  2. <strong>{Q:lastname}</strong>, {Q:firstname}
  3. </template>

Notice the placeholders {Q:lastname} and {Q:firstname}. As you may have guessed, these will get replaced by actual values of variables lastname and firstname. There placeholders can be used anywhere in the template; it is therefore very easy to fill in parts of text, URL, CSS classes, etc. At the same time, you are keeping HTML code separate from data, which is a great thing - and any software architect and webdeveloper will agree.

templateId
This String parameter tells Q.Template which HTML template to use as basis for rendering.
  1. <template id="MyTemplate" data-content="QTemplate">Hello World!</template>
  2. Q.Template("MyTemplate");
data
This optional Object or Array parameter contains structured data to be rendered (e.g. a JSON response from AJAX request). Note that Q.Template always expects the data to be an Array, and if the data provided are an Object, Q.Template transforms it into an Array whose only item is the Object you have provided.
  1. [
  2. {lastname:"Doe", firstname:"John"},
  3. {lastname:"Smith", firstname:"Bob"}
  4. ]
  1. {lastname:"Doe", firstname:"John"}
  1. [
  2. {lastname:"Doe", firstname:"John"}
  3. ]
filter
This optional function parameter may contain advanced logic that filters the data and only performs templating on those that match the function's criteria. The function is called for each data item and receives the item's data as parameter. Should the function return true, the record gets displayed. Should it return false, the record gets skipped.
  1. var MyData = [
  2. {lastname:"Doe", firstname:"John"},
  3. {lastname:"Smith", firstname:"Bob"}
  4. ];
  5. Q.Template(
  6. "MyTemplate",
  7. MyData,
  8. function(data) {
  9. if(data.lastname==='Smith') {
  10. return true;
  11. }
  12. return false;
  13. }
  14. );

The very simple filter function above would return true only for records where the lastname is "Smith", and so only the "Bob Smith" item would get rendered.

  The example above is a very simple one; your filter functions may be much more complex, even up to the point where they perform AJAX requests, themselves.

Q.Template, naturally, doesn't magically understand where you want the results displayed. To actually print them out into your web page, you'll need to use jQuery's function $.html(). For the sake of being thorough, let's take a look at what a very plain page with a very plain use of Q.Template looks like:

  1. <html>
  2. <head>
  3. <-- contents of HEAD element -->
  4. </head>
  5. <body>
  6. <-- This DIV will become a "canvas" into which we print the results. -->
  7. <div id="MyOutput"></div>
  8. <-- This is the TEMPLATE we will use to print out variables -->
  9. <template id="MyTemplate" data-content="QTemplate">
  10. <strong>{Q:lastname}</strong>
  11. {Q:firstname} <br/>
  12. </template>
  13. <script type="text/javascript">
  14. $(document).ready(function() {
  15. var MyData = [
  16. {lastname:"Doe", firstname:"John"},
  17. {lastname:"Doe", firstname:"Jack"},
  18. {lastname:"Smith", firstname:"Bob"},
  19. {lastname:"Smith", firstname:"Bill"}
  20. ];
  21. $('#MyOutput').html(
  22. Q.Template(
  23. "MyTemplate",
  24. MyData,
  25. function(data) {
  26. if(data.lastname==='Smith') {
  27. return true;
  28. }
  29. return false;
  30. }
  31. );
  32. );
  33. });
  34. </script>
  35. </body>
  36. </html>

What happens in the example above? Well, Q.Template finds the HTML element <template> that is identified as "MyTemplate". If it didn't, it would fail without throwing a JS error, but it would record an error in the Stack, so you could retrieve it using Q.GetStack() method.

Next the method loads the data provided as 2nd parameter. Since the data is already an Array, Q.Template leaves it unchanged. Then it begins looping over the data Array. Because we have also provided a 3rd parameter (a filter), Q.Template evaluates the filter function and provides it the entire row of data.

If the filter function returns true, Q.Template proceeds with parsing the template, processing the variables and inserting their values into marked places. Then the processed HTML code is stored in local cache until processing of all data is complete.

Finally, Q.Template returns the cached results as a single string of HTML code. That is where jQuery function $.html() receives the processed data and inserts them into the right place. The example would result in this:

  1. <div id="MyOutput"></div>
  2. <strong>Smith</strong>
  3. Bob <br/>
  4. <strong>Smith</strong>
  5. Bill <br/>
  6. </div>

The actual value of this method begins to show more as you'll have more data, an more complex structures to render.

  That's right, Q.Template can render much more complex structures, including nested Objects or Arrays. A "Guide To Advanced Templating" is being prepared.

GUID (String) Q.GUID()

  Returns String

This function returns a Unique Identifier string, generated according to Version 4 UUID specification.

This method does not accept any parameters.

UXTimeParse (String) Q.UXTimeParse( "datetime" )

  Returns String

This function returns a string representation of a human-readable date and time. The function calculates how much time has passed since the provided date/time, and return a human-readable format of a time (and date when applicable).

A date/time which falls on today will only show time. A date/time which happened yesterday, will read "Yesterday at hh:mm". A date/time which happened two days ago, will read "Two days ago, at hh:mm". Anything older than that will display a date and time.

datetime
This optional String parameter should be a String representation of a timestamp. Timestamp means "number of seconds since 1/1/1970". When left empty, defaults to "now".