/usr/lib/nodejs/backbone-dirty.js is in node-backbone-dirty 1.1.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | global.__backbone_dirty__ || (global.__backbone_dirty__ = {});
// Provides a `Backbone.sync` or `Model.sync` method for the server-side
// context. Uses `node-dirty` for model persistence. Models are expected to
// have a URL prefixed by their respective collection (e.g. `/{class}/{id}`)
// and Collections retrieve their respective models based on this convention.
var _ = require('underscore');
var dbs = global.__backbone_dirty__;
module.exports = function(filename) {
var dirty = dbs[filename] = dbs[filename] || require('dirty')(filename);
// Helper function to get a URL from a Model or Collection as a property
// or as a function.
var getUrl = function(object) {
if (object.url instanceof Function) {
return object.url();
} else if (typeof object.url === 'string') {
return object.url;
}
};
// Sync implementation for `node-dirty`.
var sync = function(method, model, success, error) {
switch (method) {
case 'read':
var data,
base = getUrl(model);
if (model.id) {
data = dirty.get(base);
return data ? success(data) : error(new Error('Model not found.'));
} else {
data = [];
dirty.forEach(function(key, val) {
val && key.indexOf(base) === 0 && data.indexOf(val) === -1 && data.push(val);
});
return success(data);
}
break;
case 'create':
case 'update':
if (_.isEqual(dirty.get(getUrl(model)), model.toJSON())) {
return success({});
}
var data = _(dirty.get(getUrl(model)) || {}).extend(model.toJSON());
dirty.set(
getUrl(model),
data,
function(err) {
return err ? error(err) : success({});
}
);
break;
case 'delete':
if (typeof dirty.get(getUrl(model)) === 'undefined') {
return success({});
}
dirty.rm(
getUrl(model),
function(err) {
return err ? error(err) : success({});
}
);
break;
}
};
// Set a loaded flag to indicate whether sync can begin accessing
// the db immediately or must wait until the `load` event is emitted.
dirty.on('load', function() { dbs[filename].loaded = true });
return {
dirty: dirty,
sync: function(method, model, success, error) {
var deferred = function() { sync(method, model, success, error) };
dbs[filename].loaded ? deferred() : dirty.on('load', deferred);
}
};
};
|