Click here to Skip to main content
15,885,869 members
Articles / Web Development / ASP.NET

jLinq (LINQ for JSON) Screencast #2 – Creating Your Own Extension Methods

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
13 Aug 2009CC (ASA 2.5)1 min read 19.4K   14  
jLinq (LINQ for JSON) is a completely extensible library that allows you to create your own query methods and attach them to the core library. This screencast goes over some of the basics for creating your first extension method.

jLinq is a Javascript library that allows you to do LINQ style queries with JSON data.

Did you know that jLinq lets you extend the base library with your own methods? Did you realize that any method you create plugs right in and works with the built in jLinq features?

This screencast goes over some of the basics to creating your first extension method for jLinq.

Creating a Query Method

A Query method is what determines what records stay and which records are removed from a jLinq query. Here is a sample of what an extension method looks like:

JavaScript
jLinq.extend({
    name:"evenValues", //the name of this method
    type:"query", //the kind of method this is
    count:0, //how many parameters we expect
    method:function(query) {	

        //determine how to evaluate the value being compared
        var compare = query.when({
            "string":function() {
                return query.value.length;
            },
            "array":function() {
                return query.value.length;
            },
            "other":function() {
                return query.helper.getNumericValue(query.value);
            }
        });		

        //the actual comparison
        return compare % 2 == 0;
    }
});

You’ll notice we provide the name of this new method, the kind of method (in this instance, a query method), the expected number of parameters (not counting the field name) and then the actual method we use to determine if we keep the record.

If you don’t understand how this works exactly, then you might want to watch the screencast at the end of this post to see how this works step by step.

Operator Naming

This screencast also explains operator naming. When you extend a query method, several additional operator prefixed name methods are added to jLinq as well. Each of these perform the correct switches when they are used. For example…

JavaScript
//this method is one way to do it
jLinq.from(data.users)
    .less("age", 30)
    .or()
    .evenValues()
    .select();

//this method was generated automatically - saves us a step!
jLinq.from(data.users)
    .less("age", 30)
    .orEvenValues()
    .select();

Of course, if you name your method something like orHasSomeValue() you’re going to end up with some strangely named operator methods (for example orOrHasSomeValue()).

It’s Simple–ish

Extension methods are a little confusing at first but after you create your first one, you should be on your way. This screencast will get you started in that direction.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --