-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (70 loc) · 2.27 KB
/
index.js
File metadata and controls
87 lines (70 loc) · 2.27 KB
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
80
81
82
83
84
85
86
87
var _ = require('underscore');
var handlebars_helper = require('handlebars-helper');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Resource = require('./lib/resource.js');
var View = require('./lib/view.js');
var SolidusClient = function(options) {
if (!(this instanceof SolidusClient)) return new SolidusClient(options);
options || (options = {});
this.resources_options = options.resources_options;
this.context = options.context || {};
};
// HACK: needed by universe.js...
util.inherits(SolidusClient, EventEmitter);
SolidusClient.prototype.getResource = function(options, params, callback) {
var resource = new Resource(options, this.resources_options, params);
resource.get(function(err, response) {
callback(err, response ? response.data : null);
});
};
SolidusClient.prototype.getResources = function(resources, params, callback) {
var self = this;
var errors = null;
var result = {};
var remaining = _.size(resources);
if (!remaining) return callback(errors, result);
_.each(resources, function(options, name) {
self.getResource(options, params, function(err, data) {
if (err) {
errors = errors || {};
errors[name] = err;
} else {
result[name] = data;
}
if (!--remaining) callback(errors, result);
});
});
};
SolidusClient.prototype.renderTemplate = function(template, context, template_options, callback) {
var html, err;
template_options = template_options || {};
template_options.helpers = _.extend(handlebars_helper.helpers, template_options.helpers || {});
try {
html = template(context, template_options);
} catch (error) {
err = error;
}
callback(err, html);
};
SolidusClient.prototype.view = function(view) {
var self = this;
view.render = function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this);
return self.render.apply(self, args);
};
return view;
};
SolidusClient.prototype.render = function(view, params, callback) {
var renderer = new View(this, view);
if (!callback && _.isFunction(params)) {
callback = params;
params = null;
}
if (params) {
renderer.params(params);
}
return callback ? renderer.end(callback) : renderer;
};
module.exports = SolidusClient;