Getting Started

Hang on a second...

First things first, Linquid JS lets you to work with data in a way similar to how LINQ does in .Net.
That is, of course, not to say that Linquid JS can come close to the performance you can get using
LINQ in say C#.

The primary (and there are others) reason for this is that LINQ in .NET is capable of deferring the
execution of certain queries until it absolutely has to perform them. The big upside to this is that
it can optimize what is has to do to get the result you described, potentially removing unnecessary
steps along the way. Linquid JS is not capable of this kind of optimization, it executes what you wrote.
Therefore, developer optimization of the queries written is much more important in this system
than what you describe by writing it.

Getting Linquified

Step 1:

Include this in the <head> tag of your page
<script type="text/javascript" src="yourScriptsFolder/linquid.min.js"></script>

Step 2:

There are a few options here that we'll expound upon in just a little bit
  1. Have a script demand Linquification A.S.A.P.
  2. Linquify everything!
  3. Linquify jQuery http://jquery.com/
  4. Linquify just one thing

Step 3:

Enjoy!

Having a script demand Linquification

If you've got a script that defines a bunch of globals that is loaded before everything else you
can include the following code that tells Linquid JS (when it loads) to Linquify everything:
<script type="text/javascript">linquidAuto=true;</script>

This has the exact same effect as....

Linquifying everything

Calling linq.uify() with no parameters augments Array.prototype to expose all the methods
of Linquid JS on all instances of Array. It also figures out whether or not jQuery is loaded; if it has,
it linquifies that as well. Nice and easy. If you're not a fan of modifying prototypes, keep reading
there are alternatives.

Linquifying jQuery

Calling linq.uify$() uses $.fn.extend to register Linquid as an extension. So that you can do
fun stuff like:
$(".myStyle").Select("x=>x.id");

To get the Id of everything that has "myStyle".
Just make sure jQuery has loaded before calling.

Linquifying just one thing

Calling linq.uify(myArray) linquifies just that array, no jQuery or Array.prototype mods. And,
better yet, since every result from a set-producing Linquid method returns a Linquified array, you
can chain to your heart's content!
var myArray = [1, 4, 2, 94];
linq.uify(myArray);

//Writes:
//94
//4
//2
//to the developer console if your browser's got one
myArray.Where("x => x > 1").OrderByDescending().Each(console.log);

Of course, calling linq.uify(object) also returns an instance of the Linquified object, so you could have just as easily done:
var myArray = [1, 4, 2, 94];

//Returns [94, 4, 2]
linq.uify(myArray).Where("x => x > 1").OrderByDescending();

So what you're telling me is...

A very simple example of setting up a page using Linquid JS for everything would be
<html>
    <head>
        <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="scripts/linquid.min.js"></script>
        <script type="text/javascript">
            linq.uify();
            $.fn.extend({
		classes: function(){
		    return this.Select(function(x){return x.className.split(" ");}).Aggregate();
	        }
            });
            $(function(){
	        console.log($("body").find("div").$().SelectMany("x=>x.classes()").Distinct().Intersect(["class1"]));
		var c=$(".class1").$();
		c.Where("x=>x.classes().Contains('class2')").Each("x=>x.css({color:'red'})");
		c.Except("x=>x.classes().Contains('class2')").Each("x=>x.css({opacity:.5})");
	    });
        </script>
    </head>
    <body>
	<div class="class1">
		Class 1
	</div>
	<div class="class1 class2">
		Class 2
	</div>
	<div class="class1">
		Class 1
	</div>
    </body>
</html>

Wait a minute, how do I "Un-Linquify" something?

Glad you asked! That's pretty easy too! In that last example, myArray has been Linquified; to Un-Linquifiy it:
var myArray = [1, 4, 2, 94];
linq.uify(myArray);

//Returns [94, 4, 2]
myArray.Where("x => x > 1").OrderByDescending();

unlinq.uify(myArray);

Hey now, what's with the strings?

I shall tell you. The strings are a poor-man's lambda expression, you don't have to use them if you don't want to, any
function in Linquid JS that asks for an expression could be passed a function instead. Using functions instead of lambdas
also allows you to use closures (bonus). I tend to prefer using lambdas just for brevity, and Linquid JS caches each expression
when it gets eval'd into existence to minimize the performance hit. So...

Doing this:
myArray.Where("x => x == 5");


Is the same as:
myArray.Where(function(x) { return x == 5; });


You may use whichever one suits you best. It's all up to you.

And that's the game sports fans

Please check out the rest of the documentation and enjoy!

Last edited Sep 23, 2011 at 6:51 AM by mlorbetske, version 5

Comments

No comments yet.