Skip to content

Instantly share code, notes, and snippets.

@athap
athap / wait_for_2_req.coffee
Created February 19, 2014 03:06
Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery
class Test
waitForTwoCalls: () ->
$.when(@firstCall(), @secondCall()).then () =>
doSomeThing()
firstCall: () ->
token = $.Deferred
$.ajax({
url: foo/bar,
@athap
athap / wait_for_n_call.coffee
Last active August 29, 2015 13:56
Wait for N or Multiple or Unknown number (Depends on some logic) of ajax request using JQuery
# Makes ajax req to fetch data
class Model
initialize(id)
@id = id
load: (token) ->
$.ajax({
url: "foo/bar/#{@id}",
....
success: (response) =>
@athap
athap / truncate.rb
Created March 19, 2014 01:01
Truncate text in Ruby
MaxLength = N
valid_string = some_long_text[0,N] if some_long_text.length > N
@athap
athap / watch_file_spawn_process.js
Last active August 29, 2015 13:57
Watch File And List File Information By Spawning A Process
var fs = require('fs');
var spawnProcess = require('child_process').spawn;
var filename = 'some/file'
fs.watch(filename, function() {
var list = spawnProcess('ls', ['-lh', filename]);
var info = '';
// listener get invoked every time a chunk of data is available. Data is read in chunks
list.stdout.on('data', function(chunk) {
@athap
athap / simple_connect.js
Last active August 29, 2015 13:58
Connecting To MongoDB From Node.js Beginner's Basic
var MongoClient = require('mongodb').MongoClient;
var connectionString = 'mongodb://localhost:27017/test';
// connect to test db
MongoClient.connect(connectionString, function(err, db) {
if(err) throw err;
console.log("CONNECTED");
var q = {name: 'Super Man'};
// query superheroes collection and display result
@athap
athap / node.js
Last active August 29, 2015 14:06
Node - Represent node in the tree
// Represents the node in the tree. Will be displayed as a small circle in the browser.
// x, y --> x, y co-ordinates of the center of circle
// r --> radius of the circle
// ctx --> context of the canvas
// data --> data to be displayed (Only number)
var Node = function(x,y,r, ctx, data) {
// left child of a node
this.leftNode = null;
// right child of a node
@athap
athap / index.html
Last active August 29, 2015 14:06
Index file
<!DOCTYPE html>
<html>
<head>
<style>
.center {margin: auto; width: 50%;}
<style>
</head>
<body>
<div class='input-box'>
<input id='tree-input' type='number' placeholder='Enter number'>
@athap
athap / line.js
Created September 6, 2014 17:08
Draws a line
// Draws a line from one circle(node) to another circle (node)
var Line = function() {
// Takes
// x,y - starting x,y coordinate
// toX, toY - ending x,y coordinate
this.draw = function(x, y, toX, toY, r, ctx) {
var moveToX = x;
var moveToY = y + r;
var lineToX = toX;
var lineToY = toY - r;
@athap
athap / btree.js
Created September 6, 2014 17:17
Binary tree lofic
// Represents the btree logic
var BTree = function() {
var c = document.getElementById('my-canvas');
var ctx = c.getContext('2d');
var line = new Line();
this.root = null;
var self = this;
// Getter for root
@athap
athap / addToTree.js
Created September 6, 2014 17:22
Add to tree
var addToTree = function() {
input = document.getElementById('tree-input');
value = parseInt(input.value);
if(value)
btree.add(value);
else
alert("Wrong input");
};