Implementing JSINQ If you want that your developers can write the JavaScript code in a more declarative manner as LINQ, then you can use JSINQ in your project. Once you have downloaded the JSINQ library, and extracted it, you have to use the jsing-enumerable.js and jsinq-query.js JavaScript files, and include them in all HTML pages where JavaScript shall be used.
These two JavaScript files can be found in the build folder of the JSINQ extracted zipped file. In the same folder, you can also find sample programs as well along with the source files. To implement JSINQ for JavaScript in your project, just copy the two JSINQ files into your project's web directory, where all your HTML pages are located, and in the code of the web page you can include the files as follows:
src="jsinq-enumerable.js"> src="jsinq-query.js">
JSINQ lets you create enumerables from arrays or DOM node lists, and for that you can use jsinq.Enumerable, which is the JavaScript implementation of its counterpart's System.Linq. Enumerable. To demonstrate how we use use JSINQ to create enumerables from arrays and how we can do SQL-style querying on those, we will make an array of customer names. Then we create its enumerable object using jsinq.Enumerable. On the enumerable object we will enumerate for a condition that will list all customer names starting with alphabet 'A'. With jsinq. Enumerable we can write queries using query methods defined in Enumerable constructor, for example the where() method. The following code snippet shows this:
document.writeln('Customers names starting with A: ');
var customer = ['Raman', 'Aman', 'Popat', 'Kamal', 'Ankur', 'Manpreet', 'Sandy', 'Samit']; var enumerable = new jsinq.Enumerable(customer); var customerNamesWithA = enumerable.where(function(customer) { return name.charAt(0) == 'A'; } );
var enumerator = customerNamesWithA.getEnumerator(); while (enumerator.moveNext()) { var name = enumerator.current(); document.write(name + ''); }
Enabling SQL-style queries With Enumerable constructor you have to use query methods, but to write queries in SQL-like format, JSINQ provides with jsinq.Query constructor. Using this constructor, you can do SQL-like programming in JavaScript. The following code snippet demonstrates how you can query on the same customer array and the customer enumerable used in the previous example and fetch the names of customers having alphabet 'a' in their names. document.writeln('Customers having A in their names: ');
var query = new jsinq.Query(' \ from customer in $0 \ where customer.toLowerCase().indexOf("a") > -1 \ orderby customer descending \ select customer \ '); query.setValue(0, enumerable); var result = query.execute(); var enumerator = result.getEnumerator(); while (enumerator.moveNext()) { var name = enumerator.current(); document.write(name + ''); }
Here JSINQ's query constructor compiles the SQL-style query received as string and converts it into a JavaScript code. The JSINQ query object acts like a prepared statement, so when you create the query it isn't executed. This way you can use the same query over and over again without recompiling it. That is why placeholders like $0 are used instead of regular expressions. Now using the setValue() method you can assign our enumerable to the placeholder $0. To execute the query we use the execute() method. The variable result will have the enumerable returned from executing the query on our supplied customer enumerable that we have specified in the setValue() method.
Having gone through the above code samples, you would now appreciate how JSINQ eases the work of developers by requiring them to write less code in a more declarative manner, while programming in JavaScript; thanks to the LINQ style querying capability. We shall explore more such tools in the coming issues.
Get most out of your technology infrastructure investments with Dell
About CIOL | Media Kit | Site Map | Contact Us | Help | Write to us | Jobs@CyberMedia | Privacy Policy
Copyright © CyberMedia India Online Ltd. All rights reserved. Usage of content from web site is subject to Terms and Conditions.