Backbone.js automagic syncing of Collections and Models

The idea here is to periodically fetch() and keep the client and server Collections in sync in such a way that the consuming View(s) only get updated when a Model is added to or removed from a Collection, or the attributes of one of the Models in it change. What's done is:

  • Compare the existing Collection to the incoming set and remove every Model from the existing Collection that is not in the incoming set.
  • Compare the incoming set to the existing Collection and add every Model from the incoming set to the existing Collection that is not already there.
  • Compare each Model in the two sets and update the ones in the existing Collection to the ones in the incoming set that are different.

The first two steps compare the Model's resource_uris and the last part is done with SHA1 hashes.

window.Model = Backbone.Model.extend({
    urlRoot: BASE_API + 'model/',
    defaults: {
        foo: ''
    },
    initialize: function() {
        console.log('Model->initialize()', this);
        this.bind('change', function(model) {
            console.log('Model->change()', model);
        });
    }
});
window.ModelCollection = Backbone.Collection.extend({
    model: Model,
    url: BASE_API + 'model/',
    initialize: function() {
        console.log('ModelCollection->initialize()', this);
        this.bind('add', function(model) {
            console.log('ModelCollection->add()', model);
        });
        this.bind('remove', function(model) {
            console.log('ModelCollection->remove()', model);
        });
    }
});

window.Root = Backbone.Model.extend({
    urlRoot: BASE_API + 'root/',
    defaults: {
        models: new ModelCollection()
    },
    parse: function(data) {
        var attrs = data && data.objects && ( _.isArray( data.objects ) ? data.objects[ 0 ] : data.objects ) || data;
        var model = this;
        incoming_model_uris = _.map(attrs.models, function(model) {
            return model.resource_uri;
        });
        existing_model_uris = this.get('models').map(function(model) {
            return model.get('resource_uri');
        });
        _.each(existing_model_uris, function(uri) {
            if(incoming_model_uris.indexOf(uri) == -1) {
                model.get('models').remove(model.get('models').get(uri));
            }
        });
        _.each(incoming_model_uris, function(uri) {
            if(existing_model_uris.indexOf(uri) == -1) {
                model.get('models').add(_.detect( attrs.models, function(model) { return model.resource_uri == uri; }));
            }
        });
        _.each(attrs.models, function(model) {
            if(Sha1.hash(JSON.stringify(model)) != Sha1.hash(JSON.stringify(model.get('models').get(model.resource_uri)))) {
                model.get('models').get(model.resource_uri).set(model);
            }
        });         

        delete attrs.models;        

        return attrs;
    },
    initialize: function() {
        _.bindAll(this, 'parse');
        this.fetch();
    }
});

Update 3: looking at this afterwards, I'm not sure of the complete watertightness of the above. Perhaps the Backbone Poller project would be be a better approach.

Update 2: In order to avoid borking on an empty response, do:

var attrs = Backbone.Model.prototype.parse.apply(this, data);
if(!attrs) return;

Update: My Javascript-Fu is weak which made me not see the obvious. As suggested in Backbone.js documentation, you can call the parent's implementation like this:

Backbone.Model.prototype.method.apply(this, args);

So, instead of unnecessarily copypasting behavior from Backbone-tastypie.js, we can say:

var attrs = Backbone.Model.prototype.parse.apply(this, data);

…and still have Backbone-tastypie.js do it's parsing thing for us.

Ugh.

Tagged with:

Categorised as:


Note to self: Django/South basic usage

Not that I understand how this schema migration thing actually works. Anyway, initially, on an empty database:
./manage.py syncdb --noinput
./manage.py schemamigration YOUR_APP --initial
./manage.py migrate YOUR_APP 0001 --fake
Then, after fiddling with your models:
./manage.py schemamigration YOUR_APP --auto
./manage.py migrate YOUR_APP
That should do it. But like I said, my understanding of the system is very limited and I'm sure there are cases when this simplistic pattern just won't cut it. My needs, however, at the moment are not the most complicated.

Tagged with:

Categorised as:


Note to self: Django's Error: cannot import name Foo

If Foo is definitely defined, then this is most likely a circular import which can be avoided by dropping the "redundant" import statement and referring not directly to the Model itself, but its' name. This is known as a lazy relationship:
foo = models.ForeignKey('Foo')

Tagged with:

Categorised as:


Emacs and UTF-8 Encoding

From http://blog.jonnay.net/archives/820-Emacs-and-UTF-8-Encoding.html:
;;;;;;;;;;;;;;;;;;;;
;; set up unicode
(prefer-coding-system       'utf-8)
(set-default-coding-systems 'utf-8)
(set-terminal-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8)
;; This from a japanese individual.  I hope it works.
(setq default-buffer-file-coding-system 'utf-8)
;; From Emacs wiki
(setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
;; MS Windows clipboard is UTF-16LE
(set-clipboard-coding-system 'utf-16le-dos)
Also, add this to the beginning of your source files when working with Python (otherwise you'll get "SyntaxError: Non-ASCII character '\xc3' in file…" etc. errors):
# -*- coding: utf-8 -*-

Tagged with:

Categorised as:


autofont.py

It ain't pretty but it works. Interpolator function is from: Interpolated Lookup Tables in Python. The idea here is to generate tables like this so that text scales smoothly based on viewport width:
@media screen and (max-width: 480px) { body { font-size: 0.500000em; } }
@media screen and (min-width: 481px) and (max-width: 720px) { body { font-size: 0.750em; } }
@media screen and (min-width: 721px) and (max-width: 960px) { body { font-size: 1.000em; } }
@media screen and (min-width: 961px) and (max-width: 1200px) { body { font-size: 1.250em; } }
@media screen and (min-width: 1201px) and (max-width: 1440px) { body { font-size: 1.500em; } }
@media screen and (min-width: 1441px) and (max-width: 1680px) { body { font-size: 1.750em; } }
@media screen and (min-width: 1921px) { body { font-size: 2.000000em; } }
Here's the script:
import sys

if len(sys.argv) < 7:
    print 'usage: %s LOWEST_RESOLUTION HIGHEST_RESOLUTION SMALLEST_FONTSIZE LARGEST_FONTSIZE STEPS FONT_UNIT' % (sys.argv[0])
    sys.exit(1)

llowest_resolution = int(sys.argv[1])
highest_resolution = int(sys.argv[2])
smallest_fontsize = float(sys.argv[3])
largest_fontsize = float(sys.argv[4])
steps = int(sys.argv[5])-1
font_unit = str(sys.argv[6])

resolutions = InterpolatedArray(((1, lowest_resolution), (steps, highest_resolution)))
fontsizes = InterpolatedArray(((1, smallest_fontsize), (steps, largest_fontsize)))

print '@media screen and (max-width: %dpx) { body { font-size: %f%s; } }' % (lowest_resolution, smallest_fontsize, font_unit)

for i in range(2, steps+1):
    print '@media screen and (min-width: %dpx) and (max-width: %dpx) { body { font-size: %.3f%s; } }' % (resolutions[i-1], resolutions[i], fontsizes[i], font_unit)

print '@media screen and (min-width: %dpx) { body { font-size: %f%s; } }' % (highest_resolution, largest_fontsize, font_unit)

Note: to make this work in Internet Explorer versions < 9, include the css3-mediaqueries.js script by Wouter van der Graaf.

Tagged with:

Categorised as: