JSメモ：いろいろ

# メモ：jsのクラス

```js

/**
 * Class。
 *
 * @class Class
 * @constructor
 */
function Class() {
}

/**
 * hogeメソッドです。
 *
 * @method hoge
 */
Class.prototype.hoge = function () {

  /**
   * aaa
   *
   * @private
   * @method aaa
   * @param a {String} a
   * @param b {String} b
   * @param c {String} c
   * @return Object
   */
  var aaa = function (a, b, c) {
    return {
     a: a,
     b: b,
     c: c
    };
  };
};
```





# Ajaxのスニペット
```js

      function createHttpRequest(){
        "use strict";
        if(window.ActiveXObject){
          try{return new ActiveXObject("Msxml2.XMLHTTP");
          }catch(e){
            try{return new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e2){return null;}}
        }else if(window.XMLHttpRequest){
          return new XMLHttpRequest();}else{return null;
          }
      }
      var getdata=function(query){
        var req=new createHttpRequest();
        req.open("GET",query,false);
        req.onreadystatechange=function(){
          if(req.readyState==4){
            if(req.status==200){
              this.params=req.responseText;
            }else{
              alert('AJAX failed'+req.status+query);}
          }
        }
        req.send(null);return req.responseText;
      }

```



# express routerパラメタの強制

```

# 数字のみを受け付ける
app.get('/search/:character_id(\\d+)/:page(\\d+)',  search.advanced);
```

## 検索条件の構築コントローラの設定
```
   　　var events = require('events');
    var util = require('util');

    db.serialize(function(){
     var sql = '';
     var n = {};
     var i = 0;

     var where = [];
     for(p in req.params){
       i+=1;
       var availableKeies = ['character','level','attribute'];
       if(availableKeies.indexOf(p)>0){
         if(p != undefined){
           n["$"+p] = req.params[p];
           if( p == 'level'){
            where.push (p +'>$'+p ); 
           }else{
            where.push (p +'=$'+p );
           }
         }
       }
       if( Object.keys(req.params).length > i){
         console.log('OBJECTID:::',Object.keys(req.params).length);
       }else{
         sql = " select level users.created_at from users, characters where "+where.join(' and ')+" and users.character = characters.character_id   --;";

         process.nextTick(function(){
           db.prepare(sql);
           db.all(sql, n, function(err, rows_user){
             data.result = rows_user || {};
             process.nextTick(function(){
              res.render('search',{data:data, p:p });
              });
           });
          });
       }
     };
```




#twitterからの検索
```js

/*
* Twitter検索
*
*/
$().ready(function(){
  $('#form').submit(function(){
    var query = $(this).children('input').val();
    if (!query) return false;
    $.ajax({
      url : 'http://search.twitter.com/search.json',
      data : {
        q : query + ' instagr.am',
        lang : 'ja',
        rpp : '100',
        //include_entries: 1,
        result_type:'mixed'
      },
      dataType : 'jsonp',
      success : function(json){
        var entries = json.results;
        $('#main').html('');
        if (!entries) return;
          console.log(json);
        $.each(entries, function(){
          if(this.entities && this.entities.urls[0]){
            var url= this.entries.urls[0].expanded_url;
            if(url.match(/instagr\.am\/p\/(\w+)/)){
              var id = RegExp.$1;
              var a = $('&lt;a/&gt;').attr('href','http://instagr.am/p/' + id + '/');
              a.attr('target','_blank');
              var img = $('&lt;img/&gt;').attr('src','http://instagr.am/p/' + id + '/media/?size=t');
              img.bind('load',function(){
                a.hide();
                a.append(img);
                $('#main').prepend(a);
                a.feadIn();
              });
            }
          }
        });
      },
    });
    return false;
  });
});