0

I have requirement where I need to run some validations based on json data about controls. I have a file in JSON format in below format

{
"Activity List View" :[
    {
        "Activity Form Applet":{
            "Asset Number": {"type":"text","minlength":5},
            "Type":{"type":"text","maxlength":5},
            "Comments":{"type":"text","email":"true"},
            "Status":{"type":"number","maxlength":9},
            "Priority":{"type":"text","minlength":5}
        }
    }
]   
}

Basically I need to loop around this data and perform the validations as mentioned in the data and highlight the field if a validation fails.

I am new to jQuery so not aware how to go about it. As per my knowledge I can approach it in two ways.

  1. Create a library of function that accept certain input and perform actions. It will be collections of functions like findElement, maxLength, minLength and so on.

  2. Create a jQuery plugin and add same set of functions in plugin that performs the same functionality.

Due to my inexperience in jQuery the time required to complete will increase many fold. So I would like to know

What are the benefits of doing it jQuery plugin way rather than a library and are the benefits worth it?

Edit: based on the answers I can understand the benefits of sharing the plugin but are there any noticeable benefits in terms of Memory and performance (when used heavily)??

Neel
  • 613
  • 4
  • 14
  • 32

4 Answers4

1

Disclaimer: never wrote a jQuery plugin myself - but I use jQuery.

IMHO, if you write a jQuery plugin it would be because you want to share it with the world...it could be published as such and others could install it.

If your functions are for your specific case only, then you don't need to write them as a plugin.

Probably there are other aspects to consider but that's what jumped at me

transient_loop
  • 5,984
  • 15
  • 58
  • 117
1

Essentially there is no difference where you attach your functions to the jQuery object '$' or your own library.

Either way you can use jQuery which has many functions which will make the validation easier.

ioseph
  • 1,887
  • 20
  • 25
  • What has this to do with external files? – Bergi Mar 06 '14 at 05:21
  • Edited answer to clarify – ioseph Mar 06 '14 at 05:26
  • Any benefits in terms of performance and memory usage ?? – Neel Mar 06 '14 at 05:38
  • In terms of where your defined, no. As for using jquery methods, I'd say they'll generally perform better than writing your own, however if it's for a very specific application you could probably write a more efficient validator. I'd highly recommend using jQueries JSON parser and hazard to say validation is generally a cheap operation compared to say DOM manipulation. – ioseph Mar 06 '14 at 06:40
1

If you are writing something that is dependent upon jQuery, then a jQuery plugin would be a good approach. Meaning, you had to have something jQuery already provides and you'd reinvent the wheel otherwise.

If you are mainly looking at the length and various Regular Expressions, then I would write a closure. Here's an example using the revealing module pattern, which is how I'd approach this: http://jsfiddle.net/E2Wug/1/

The benefit here is you'd have the encapsulation for portability (and may be what you mean when you say library). Since most of the logic doesn't require jQuery, you may prefer to not have to rely upon it. If something new and shiny comes out that doesn't play well with jQuery, then you only have to change the logic within the validateForm function.

My view is you want to make your code library agnostic. Given dojo, yui, jQuery, sencha, etc., this is always evolving and you want to maximize re-use.

Here's an example of using said library:

$(document).ready(function () {
    $('.save').on('click', function () {
        if (vdl.validateForm()) {
            $('#log').html('Form is valid');
        } else {
            $('#log').html('Error: ' + vdl.getError());
        }
    });

    vdl.setValidation(validationMetaData);
});
Paul Way
  • 1,966
  • 1
  • 13
  • 10
  • What do you consider "reinventing the wheel"? A library could be dependent on jQuery without being a plugin. And what is a "normal closure"? – Bergi Mar 06 '14 at 05:23
  • Some background to a JavaScript closure [Intro](http://www.javascriptkit.com/javatutors/closures.shtml) [What StackOverflow says](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Paul Way Mar 06 '14 at 05:33
  • I know what a closure is, and that it does not help here. What do you mean, a module IEFE? – Bergi Mar 06 '14 at 05:59
  • @Bergi As for the wheel, if the author doesn't plan to use jquery within the logic of the plugin/library, then making a plugin would seem backwards. – Paul Way Mar 06 '14 at 06:09
  • @Bergi I feel like you are trolling me here, a closure allows you to encapsulate your code and provide proper namespace for the library... – Paul Way Mar 06 '14 at 06:25
  • A namespace is done by building an object, not by creating a closure. Yes, closures do help with encapsulation, but a [*(revealing) module*](http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript) - what you seem to mean - is much more than that. Please use the correct terms. – Bergi Mar 06 '14 at 12:33
1

What are the benefits of doing it jQuery plugin way rather than a library?

You've got your methods directly available on jQuery DOM collection instances. That's everything. Oh, and you can call it a jQuery plugin, which might be good for marketing…

are the benefits worth it?

Whether that difference is a benefit depends on your problem. For everything that only does operate on DOM elements a plugin can be easily and homogenously integrated in existing jQuery code.

However, if you need internal state or create some custom objects, especially if not bound to a single DOM element, a freestanding library will be the better suited. You can do that with jQuery plugins as well (called "widgets"), but their code pattern gets ugly.

are there any noticeable benefits in terms of Memory and performance (when used heavily)??

Hardly any differences in regard to memory. Of course there might be some depending on your exact implementation, but either could be coded badly. Everything that does not use jQuery can be faster than something that uses jQuery, but as you say that is only relevant when used really heavily - and a validator as in your question is defintely not.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375