abemassry

       GitHub: @abemassry
  • One liner to print all of unicode

    abemassry        
    0 Likes Commentssh

    for i in `bc <<< "obase = 16 ; $(seq 0 65536)" | tr '\n' ' '`; do printf "$i:"; printf "\u$i"; printf '\n'; done | rs -e | less
  • Smile

    abemassry        
    0 Likes Commentsjson

    {"Hi": "😀"}
  • Socket.io listen to a socket on the client

    abemassry        
    0 Likes Commentsjavascript

    var socket = io.connect('http://example.com');
    socket.on('list_update', function(data) {
      console.log("update client with: " + data);
    });
  • Socket.io send a message from the server

    abemassry        
    0 Likes Commentsjavascript

    io.sockets.emit('list_update', data);
  • Update: Added Delete Button for Comments

    abemassry        
    0 Likes Commentstext

    Now users can delete their own comments that they have submitted.
  • Update: Added Delete Button

    abemassry        
    0 Likes Commentstext

    I've added a delete button on the Edit page, so users can now delete their codestagrams.
  • Update: Editing Enabled

    abemassry        
    1 Likes Commentstext

    When users go to a codestagram that they personally submitted there will be an Edit link.  
    They can then edit the Title, Code, Language, and apply a different filter if they'd like.
  • HTML5 Local Storage

    abemassry        
    2 Likes Commentsjavascript

    function supports_html5_storage() {
      try {
        //if it worked then you can write something to local storage
        localStorage["works"] = "yes";
        return 'localStorage' in window && window['localStorage'] !== null;
      } catch (e) {
        return false;
      }
    }
    
    $(document).ready(function() {
      $("#login_button_pressed").click(function(){
        //if not false then it means it worked
        if (supports_html5_storage() !== false) {
          //write key value pairs to local storage
          localStorage["code_stored"] = 1;
          localStorage["title"] = $("#title").val();
          localStorage["lang"] = $("#lang").val();
          localStorage["filter"] = $("#filter").val();
          localStorage["code"] = editor.getValue();
        }
        $("#code_holder").val(editor.getValue());
      });
    });
    
  • Mongoose and node.js query two fields at once from mongodb

    abemassry        
    1 Likes Commentsjavascript

    models.articles.find( { $or: [ {title: query}, {body: query}]}).limit(30).sort('-date').exec(function(err, data){
      if(err) { throw err; }
      res.render('results.jade', {
        locals: {
          page: 'results',
          results: data,
          query: query
        }
      });
    });
  • Handling 404 pages with express and node.js

    abemassry        
    1 Likes Commentsjavascript

    app.get('/404', function(req, res){
        
        if (req.user) {
          var username = req.user.username; 
        }
        
        res.render('404.jade', {
            locals: {
              page: 'Not Found',
              user: username
            }
        });
      });
      
      //last function before end of file
      app.use(function(req,res){
        res.redirect('/404');
      });
  • Client side querystring javascript jquery, to put querystring on the page

    abemassry        
    0 Likes Commentsjavascript

    $(document).ready(function() {
      q = urldecode(getQuerystring('q'));
      $('#search').val(q);
    });
  • Jade head section, stylesheets and scripts

    abemassry        
    0 Likes Commentsjade

    meta(name="viewport", content="width=device-width, initial-scale=1.0")
    link(rel="icon", href="/img/favicon1.png")
    link(rel='stylesheet', href='/stylesheets/bootstrap.min.css')
    link(rel='stylesheet', href='/stylesheets/bootstrap-responsive.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
    link(rel="stylesheet", type="text/css", href="/stylesheets/zocial.css")
    link(rel="stylesheet", href="/stylesheets/auth-buttons.css")
    link(href='http://fonts.googleapis.com/css?family=Pompiere', rel='stylesheet', type='text/css')
    //-link(rel='stylesheet', href='//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.min.css')
    //-link(rel='stylesheet', href='//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap-responsive.min.css')
    
    script(src='//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js')
    script(src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js")
    script(src="/javascripts/jquery.timeago.js")
  • Reading subdoc in jade and outputting to browser nodejs

    abemassry        
    0 Likes Commentsjade

    - each like in code.likes
      - i++;
    button.btn.like(type="button", id="like_#{code.id}")
      i.icon-thumbs-up
      | Like
    - each comment in code.comments
      - j++;
  • Reading from nested doc in mongoose nodejs

    abemassry        
    0 Likes Commentsjavascript

    app.get('/code/:id', function(req, res){
    
        if (req.user) {
          var username = req.user.username;
        }
    
        //if (app.requireAuth === true && req.loggedIn === false)
          //res.redirect('/auth/twitter');
    
        //get the example
        models.codes.findById(req.params.id, function(err, data){
          if (data) {
            models.users.findOne({name: username}, function(err, userdata){
             //render the view page
             res.render('code.jade', {
                 locals: {
                   title: data.title,
                   page: '',
                   code: data,
                   user: username,
                   userdata: userdata
                 }
             });
            });
          } else {
            res.redirect('/404');
          }
    
        });
      });
    
  • Write to a nested schema nodejs mongoose using push

    abemassry        
    0 Likes Commentsjavascript

    app.post('/comment', function(req, res){
        if (req.user) {
          models.users.findOne({name: req.user.username}, function(err, userdata){
            var picture = userdata.photo;
            var now = new Date();
            code_id = req.param('code_id');
            models.codes.findById(code_id, function(err, code){
              if (err) return handleError(err);
              code.comments.push({user: req.user.username, user_photo: picture, body: req.param('comment'), date_commented: now});
              var doc = code.comments[0];
              console.log(doc);
              code.save(function(err) {
                console.log('error check');
                if(err) { throw err; }
                console.log('saved');
                res.redirect('/code/'+code_id);
              });
            });
          });
        }
      });
    
  • mongoose, mongodb, nodejs nested schema

    abemassry        
    0 Likes Commentsjavascript

      var Likes = new Schema({
        like_id: ObjectId,
        name: String,
        date_liked: Date,
      });
      exports.Likes = mongoose.model('Likes', Likes);
    
      var Comments = new Schema({
        comment_id: ObjectId,
        user: String,
        user_photo: String,
        body: String,
        date_commented: Date,
      });
      exports.Comments = mongoose.model('Comments', Comments);
    
      var schema = new Schema({
        code_id: ObjectId,
        body: String,
        lang: String,
        filter: String,
        title: String,
        date: Date,
        submitted_by: String,
        submitted_by_photo: String
        , likes: [Likes],
        comments: [Comments]
      });
  • Node.js package.json dependencies for npm

    abemassry        
    0 Likes Commentsjson

    {
      "dependencies": {
        "express": "2.5.8",
        "jade": ">= 0.0.1",
        "mongodb": ">= 0.9.6-7",
        "mongoose": ">= 2.5.10",
        "everyauth": ">=0.2.32",
        "jsdom": ">=0.2.15",
        "request": ">=2.11.4",
        "xml2js": ">=0.2.0",
        "async": ">=0.1.22",
        "now": "0.8.1",
        "passport": "0.1.15",
        "passport-github": "0.1.3",
        "util": "0.4.9"
      },  
      "engines": {
        "node": ">= 0.8.x",
        "npm": "1.1.x"
      }
    }
  • HTML5 Local Storage

    abemassry        
    0 Likes Commentsjavascript

    function supports_html5_storage() {
      try {
        localStorage["works"] = "yes";
        return 'localStorage' in window && window['localStorage'] !== null;
      } catch (e) {
        return false;
      }
    }
    
    $(document).ready(function() {
      $("#login_button_pressed").click(function(){
        if (supports_html5_storage() !== false) {
          localStorage["code_stored"] = 1;
          localStorage["title"] = $("#title").val();
          localStorage["lang"] = $("#lang").val();
          localStorage["filter"] = $("#filter").val();
          localStorage["code"] = editor.getValue();
        }
        $("#code_holder").val(editor.getValue());
      });
    });
  • Reading subdoc in jade and outputting to browser nodejs

    abemassry        
    0 Likes Commentsjade

    - each like in code.likes
      - i++;
    button.btn.like(type="button", id="like_#{code.id}")
      i.icon-thumbs-up
      | Like
    - each comment in code.comments
      - j++;