Go App is my the first test application using Go lang

Go page sources

package app

import (
    "github.com/garyburd/twister/server"
    "github.com/garyburd/twister/web"
    "text/template"
)

var (
  page *template.Template
)

func index(request *web.Request) {
  page.Execute(
    request.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8"),
    request.URL.Host)
}

func main() {
  page = template.Must(template.New("index").Parse(body))

  server.Run(":8080",
    web.NewRouter().
      Register("/", "GET", index))
}

const body = `
<html>
  <head>
    <title>Developed using Go :)</title>
  </head>
  <body>
    <h1>I made this page using Go language</h1>
  </body>
</html> `
1 note

JSConf 2012
0 notes

html5 offline application

2 days ago I’ve lost my internet connection at home. But I had very important emails for me on the gmail. But I thought I have offline application for gmail developed by google.

I’ve installed this chrome app just for checking possible implementation for offline apps. because on my current work we are working on the creation with the same functionnality application. Offline gmail is using web sqlite storage for saving cached / synced data.

it looks like

Time to completely client side applications. Yeah, it’s really awesome. But for less complex applications it’s pretty simple to use localStorage for saving synced data.

Also as I see guys in google added syncing process on the background and refresh button for manually syncing by user action. 

For opening application they are using html5 application manifest for caching resources like css/js/images. In Rails we are using rack-offline for generating same manifest file choosing files for caching.

10 notes

backbone js snippets

Hi, everyone.

Not far a long time ago I’ve started to work on one project where we was using backbone for creating offline website version. It means we have a goal to create possibility to delay our requests to the server and works without postbacks.

We took example with backbone localstorage from backbone docs website(http://documentcloud.github.com/backbone/docs/backbone-localstorage.html). it’s very nice sources. But I’ve found that if you will decide to clear properly local storage you need to create extension.

The first extension for more pleasure removing keys from localstorage:


Store.destroy = function() {
  var models = [].slice.call(arguments, 0);
  for(var i = 0; i < models.length; i++)
    localStorage.removeItem(models[i]);
};

The second extension for changing removing collection from localstorage using sync method. As you can at first I am iterating through collection and then executing save method for localstorage.


_.extend(Store.prototype, {
  // ...
  destroy_all: function(collection) {
    var self = this;
    collection.each(function(model) {
      delete self.data[model.id];
    });
    this.save();
    return collection;
  }
});

Backbone.sync = function(method, model, options) {
  // ...
    case "delete_all": resp = store.destroy_all(model);
  }
  // ...
};

Backbone.Collection.prototype.destroy_all = function(options) {
  options = options || {};
  var collection = this;
  var success = options.success;
  options.success = function(resp) {
    collection.trigger('destroy_all', collection, options);
    if (success) success(collection, resp);
  };
  options.error = (function(onError, c, options) {
    return function(resp) {
      if (onError) {
        onError(model, resp, options);
      } else {
        model.trigger('error', c, resp, options);
      }
    };
  })(options.error, collection, options);
  return (this.sync || Backbone.sync).call(this, 'delete_all', this, options);
};

Also I want to provide very small snippet how to override fetch method for collecitons. Sometimes it’s very helpfull for filtering values by another collection. Let’s check example:


var Post = Backbone.Model.extend({
  initialize: function() {
    this.localStorage = new Store('posts');
  }
});

var Comment = Backbone.Model.extend({
  initialize: function() {
    this.localStorage = new Store('comments');
  }
});

var Comments = Backbone.Collection.extend({
  model: Comment,
  
  initialize: function(posts) {
    this.localStorage = new Store('comments');
    this.posts = posts;
    
    this._parent_fetch = Backbone.Collection.prototype.fetch;
  },
  
  fetch: function() {
    options = options || {};
    var self = this;

    this._parent_fetch.call(this, {
      success: function() {
        self.reset(self.find_by_posts(self.posts));
        options.success.call(self, self);
      },
      error: options.error
    });
  },
  
  find_by_posts: function(posts) {
    var ids = posts.map(function(post) { return post.id; });

    return this.filter(function(comment) {
      return _.include(ids, comment.get('post_id'));
    });
  }
});


// Example:
var posts = new Posts();
posts.fetch({
  success: function() {
    var comments = new Comments(posts.models);
    comments.fetch({
      success: function() {
        comments.each(function(comment){
          // show comments related to posts
          console.log(comment.id);
          console.log(comment.get('post_id'));
        });
      }
    });
  }
});

That’s it

20 notes

Amazon S3 client is my latest application based on nodejs.
http://meds.no.de/

Amazon S3 client is my latest application based on nodejs.

http://meds.no.de/

13 notes

Small snippet for C# for getting PublicKey of assembly

using(var writer = new StreamWriter(new FileStream("data.txt", FileMode.CreateNew, FileAccess.Write)))
{
    foreach (var b in Assembly.GetAssembly(typeof(Program)).GetName().GetPublicKey())
    {
        writer.Write(String.Format("{0:x2}", b));
    }
}
5 notes