-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeDeferredProvider.js
105 lines (96 loc) · 2.74 KB
/
makeDeferredProvider.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* makeDeferredProvider - a small function to augment the visualforce
* Remote Objects "SObjectModel" object to return an object
* that provides promises
*
* Copyright (c)2014, Matt Welch.
* License: MIT
*
* Usage:
* Call 'makeDeferredProvider("RemotingJSNameSpace")' (the argument is optional,
* and will default to "SObjectModel") to prepare the remoting
* object to provide promise-generating objects. Then, instead of
* var c = RemotingJSNameSpace.contact();
* use
* var c = RemotingJSNameSpace.deferredObject('contact');
*
*/
var makeDeferredGlobal = (function () {
return this || (1, eval)('this');
}());
function makeDeferredProvider(nameSpace) {
// Default to "SObjectModel", which is the VF Remote Object default as well
nameSpace = nameSpace ? nameSpace : "SObjectModel";
// Add a function to our VF Remote Objects object
makeDeferredGlobal[nameSpace].deferredObject=function(obj,vars) {
var promiseRemoteObject={};
promiseRemoteObject.remoteObject=new this[obj](vars);
promiseRemoteObject.set=function(f,v) {
this.remoteObject.set(f,v);
}
promiseRemoteObject.get=function(f) {
return this.remoteObject.get(f);
}
promiseRemoteObject.retrieve=function(opts) {
var deferred = $.Deferred();
this.remoteObject.retrieve(opts, function(err, records) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(records);
}
});
return deferred.promise();
}
promiseRemoteObject.create=function(fvs) {
var deferred = $.Deferred();
fvs = fvs ? fvs:this.remoteObject._props;
this.remoteObject.create(fvs,function(err, result, e) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(result, e);
}
});
return deferred.promise();
}
promiseRemoteObject.update=function(ids,fvs) {
var deferred = $.Deferred();
// If our ids object isn't an array, the user probably didn't want to include ids
// so let's assume that the first argument should be our field-value pairs instead
if (!(ids instanceof Array)) {
fvs=ids;
ids=null;
}
ids = ids ? ids : [this.remoteObject._props.Id];
fvs = fvs ? fvs:this.remoteObject._props;
console.log(this.remoteObject._props);
console.log(ids);
this.remoteObject.update(ids,fvs,function(err, ids) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(ids);
}
});
return deferred.promise();
}
promiseRemoteObject.del=function(ids) {
var deferred = $.Deferred();
ids = ids ? ids : [this.remoteObject._props.Id];
this.remoteObject.del(ids,function(err, ids) {
if (err) {
deferred.reject(err);
}
else {
deferred.resolve(ids);
}
});
return deferred.promise();
}
return promiseRemoteObject;
};
}