forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire.js
81 lines (62 loc) · 1.87 KB
/
wire.js
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
/*
* Copyright 2012-2013 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
(function (define) {
'use strict';
define(function (require) {
var RestStore, clientPlugin, mixin, when;
RestStore = require('./RestStore');
clientPlugin = require('../wire');
mixin = require('../util/mixin');
when = require('when');
return {
wire$plugin: function restPlugin(/* ready, destroyed, options */) {
var plugin;
plugin = clientPlugin.wire$plugin.apply(clientPlugin, arguments);
Object.keys(plugin).forEach(function (key) {
if (typeof plugin[key] === 'object') {
plugin[key] = mixin({}, plugin[key]);
}
});
/**
* Resolves a RestStore client for the specified path and scopes, e.g. resource!url/to/resource
*
* @param resolver
* @param name
* @param refObj
* @param wire
*/
function resolveResource(resolver, name, refObj, wire) {
var client;
client = when.defer();
plugin.resolvers.client(client.resolver, name, refObj, wire);
client.promise.then(function (client) {
var args, store;
args = { client: client };
if (refObj.idProperty) { args.idProperty = refObj.idProperty; }
store = new RestStore(args);
if (refObj.get) {
store.get(refObj.get).then(resolver.resolve, resolver.reject);
}
else if (refObj.query) {
store.query(refObj.query).then(resolver.resolve, resolver.reject);
}
else {
// Neither get nor query was specified, so resolve with
// the store itself.
resolver.resolve(store);
}
});
}
plugin.resolvers.resource = resolveResource;
return plugin;
}
};
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));