01
Jun 08

jQuery.NameSpace

This plugin works for provide to your code a structure of namespaces.

Download it here: jQuery.NameSpace (2.15 KB) - 493 hits

With a simple way to use, we can create a code like this:

 HTML |  copy code |? 
01
02
<script language="JavaScript">
03
<!--
04
  $.namespace("validate", {
05
    isFullName: function(elems){
06
      return elems.val().indexOf(" ") > -1;
07
    },
08
    isNick: function(elems){
09
      return elems.val().indexOf(" ") == -1;
10
    },
11
    isEmail: function(elems){
12
    	return elems.val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
13
    }
14
  });
15
 
16
  validators = { "fullname": "isFullName", "nick": "isNick", "email": "isEmail" };
17
  checkForm = function(){
18
    res = true;
19
    $("#frm").each(function(){
20
      $("input").each(function(){
21
        if($(this).attr("data") != undefined)
22
          if(res)
23
            res = eval("$(this).validate." + validators[$(this).attr("data")] + "()");
24
      });
25
    });
26
 
27
    if(res){
28
      alert('You complete the form with a regular informations.');
29
    }else{
30
      alert('Complete your form.');
31
    }
32
  }
33
 
34
  checkFullName = function(){ 
35
    ($("#myFullName").validate.isFullName())? alert('Valid'): alert('invalid');
36
  }
37
  checkNick = function(){ 
38
    ($("#myNick").validate.isNick())? alert('Valid'): alert('invalid');
39
  }
40
  checkEmail = function(){ 
41
    ($("#myEmail").validate.isEmail())? alert('Valid'): alert('invalid');
42
  }
43
 
44
  $.namespace("ns", {
45
    inherited: true,
46
    one: function(){
47
      return this.each(function(){
48
        $(this).css("left", '10px')
49
      });
50
    },
51
    two: function(){
52
      return this.each(function(){
53
        $(this).css("top", '10px')
54
      });
55
    }
56
  });
57
 
58
  $(function(){
59
    $('#testss').ns.one().ns.two();
60
  });
61
//-->
62
</script>
63
<body>
64
<form id="frm">
65
<table>
66
<tr><td>FullName:</td><td><input type="text" value="" id="myFullName" data="fullname"></td></tr>
67
<tr><td>Nick:</td><td><input type="text" value="" id="myNick" data="nick"> (no space accepted)</td></tr>
68
<tr><td>E-mail:</td><td><input type="text" value="" id="myEmail" data="email"></td></tr>
69
</table>
70
<input type="button" onclick="checkForm()" value="check">
71
</form>
72
<input type="button" onclick="checkFullName()" value="check FullName">
73
<input type="button" onclick="checkNick()" value="check Nick">
74
<input type="button" onclick="checkEmail()" value="check Email">
75
 
76
<div id="testss" style="position:relative;top:20;left:20;background:#ff0;">TEST NAMESPACE</div>
77
</body>
78

Will result on this:

FullName:
Nick: (no space accepted)
E-mail:

TEST NAMESPACE

Nice?
Feel free to use and comment.


9 comments

  1. Marc van Neerven

    For your information, I posted a bugfix on plugins.jquery.com, http://plugins.jquery.com/node/5995

    Marc

  2. Marc van Neerven

    Fixed some issues with your plugin (http://plugins.jquery.com/node/5995)

  3. Marc, I’ve updated jquery.namespace to 2008.0.1.2
    Thanks for the suggestion.

  4. Hey Gilverto. Good stuff! One recommendation. var both the nss and snss variables so they don’t pollute the global namespace. Thanks. -Dan

  5. Dan,

    Will be the next step of jQuery.NameSpace.

    Thanks for the suggestion.

  6. The download does not appear to be working.

  7. Gilberto Saraiva

    Oh my! really sorry, I’ve fixed it now.

  8. João Marcus Christ

    How can I let the namespaced methods accept parameters?

  9. Gilberto Saraiva

    inherited property enables the correctly call for the function.

     Javascript |  copy code |? 
    1
      $.namespace("customCss", {
    2
        inherited: true,
    3
        SetColor: function(color){
    4
          return this.each(function(){
    5
            $(this).css("backgroundColor", color);
    6
          });
    7
        }
    8
      });

    Without inherited param, you have to manipulate params as a array, like the example below:

     Javascript |  copy code |? 
    1
      $.namespace("customCss", {
    2
        SetColor: function(elem, params){
    3
          return elem.each(function(){
    4
            $(this).css("backgroundColor", params[0]);
    5
          });
    6
        }
    7
      });

    The use for both:

     Javascript |  copy code |? 
    1
    $(function(){
    2
      $('#CustomDiv').customCss.SetColor("green");
    3
    }

Leave a comment