Skip to content

Commit a756f72

Browse files
committed
Re-implemented bindAll and bindOne.
1 parent 3e16042 commit a756f72

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed

dist/js-data-angular.js

+82
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
.provider('DS', function () {
9898

9999
var _this = this;
100+
var DSUtils = JSData.DSUtils;
101+
var DSErrors = JSData.DSErrors;
100102
var deps = [];
101103

102104
for (var i = 0; i < adapters.length; i++) {
@@ -107,18 +109,87 @@
107109

108110
_this.defaults = {};
109111

112+
JSData.DS.prototype.bindAll = function (scope, expr, resourceName, params, cb) {
113+
var _this = this;
114+
115+
params = params || {};
116+
117+
if (!DSUtils.isObject(scope)) {
118+
throw new DSErrors.IA('"scope" must be an object!');
119+
} else if (!DSUtils.isString(expr)) {
120+
throw new DSErrors.IA('"expr" must be a string!');
121+
} else if (!_this.definitions[resourceName]) {
122+
throw new DSErrors.NER(resourceName);
123+
} else if (!DSUtils.isObject(params)) {
124+
throw new DSErrors.IA('"params" must be an object!');
125+
}
126+
127+
try {
128+
return scope.$watch(function () {
129+
return _this.lastModified(resourceName);
130+
}, function () {
131+
var items = _this.filter(resourceName, params);
132+
DSUtils.set(scope, expr, items);
133+
if (cb) {
134+
cb(null, items);
135+
}
136+
});
137+
} catch (err) {
138+
if (cb) {
139+
cb(err);
140+
} else {
141+
throw err;
142+
}
143+
}
144+
};
145+
146+
JSData.DS.prototype.bindAll = function (scope, expr, resourceName, id, cb) {
147+
var _this = this;
148+
149+
id = DSUtils.resolveId(_this.definitions[resourceName], id);
150+
if (!DSUtils.isObject(scope)) {
151+
throw new DSErrors.IA('"scope" must be an object!');
152+
} else if (!DSUtils.isString(expr)) {
153+
throw new DSErrors.IA('"expr" must be a string!');
154+
} else if (!DS.definitions[resourceName]) {
155+
throw new DSErrors.NER(resourceName);
156+
} else if (!DSUtils.isString(id) && !DSUtils.isNumber(id)) {
157+
throw new DSErrors.IA('"id" must be a string or a number!');
158+
}
159+
160+
try {
161+
return scope.$watch(function () {
162+
return _this.lastModified(resourceName, id);
163+
}, function () {
164+
var item = _this.get(resourceName, id);
165+
DSUtils.set(scope, expr, item);
166+
if (cb) {
167+
cb(null, item);
168+
}
169+
});
170+
} catch (err) {
171+
if (cb) {
172+
cb(err);
173+
} else {
174+
throw err;
175+
}
176+
}
177+
};
178+
110179
function load() {
111180
var args = Array.prototype.slice.call(arguments);
112181
var $rootScope = args[args.length - 1];
113182
var store = new JSData.DS(_this.defaults);
114183
var originals = {};
115184

185+
// Register any adapters that have been loaded
116186
for (var i = 0; i < adapters.length; i++) {
117187
if (adapters[i].loaded) {
118188
store.registerAdapter(adapters[i].name, arguments[i]);
119189
}
120190
}
121191

192+
// Wrap certain sync functions with $apply
122193
for (i = 0; i < functionsToWrap.length; i++) {
123194
originals[functionsToWrap[i]] = store[functionsToWrap[i]];
124195
store[functionsToWrap[i]] = (function (name) {
@@ -134,6 +205,17 @@
134205
})(functionsToWrap[i]);
135206
}
136207

208+
// Hook into the digest loop (throttled)
209+
if (typeof Object.observe !== 'function' ||
210+
typeof Array.observe !== 'function') {
211+
$rootScope.$watch(function () {
212+
// Throttle angular-data's digest loop to tenths of a second
213+
return new Date().getTime() / 100 | 0;
214+
}, function () {
215+
store.digest();
216+
});
217+
}
218+
137219
return store;
138220
}
139221

dist/js-data-angular.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+82
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
.provider('DS', function () {
8888

8989
var _this = this;
90+
var DSUtils = JSData.DSUtils;
91+
var DSErrors = JSData.DSErrors;
9092
var deps = [];
9193

9294
for (var i = 0; i < adapters.length; i++) {
@@ -97,18 +99,87 @@
9799

98100
_this.defaults = {};
99101

102+
JSData.DS.prototype.bindAll = function (scope, expr, resourceName, params, cb) {
103+
var _this = this;
104+
105+
params = params || {};
106+
107+
if (!DSUtils.isObject(scope)) {
108+
throw new DSErrors.IA('"scope" must be an object!');
109+
} else if (!DSUtils.isString(expr)) {
110+
throw new DSErrors.IA('"expr" must be a string!');
111+
} else if (!_this.definitions[resourceName]) {
112+
throw new DSErrors.NER(resourceName);
113+
} else if (!DSUtils.isObject(params)) {
114+
throw new DSErrors.IA('"params" must be an object!');
115+
}
116+
117+
try {
118+
return scope.$watch(function () {
119+
return _this.lastModified(resourceName);
120+
}, function () {
121+
var items = _this.filter(resourceName, params);
122+
DSUtils.set(scope, expr, items);
123+
if (cb) {
124+
cb(null, items);
125+
}
126+
});
127+
} catch (err) {
128+
if (cb) {
129+
cb(err);
130+
} else {
131+
throw err;
132+
}
133+
}
134+
};
135+
136+
JSData.DS.prototype.bindAll = function (scope, expr, resourceName, id, cb) {
137+
var _this = this;
138+
139+
id = DSUtils.resolveId(_this.definitions[resourceName], id);
140+
if (!DSUtils.isObject(scope)) {
141+
throw new DSErrors.IA('"scope" must be an object!');
142+
} else if (!DSUtils.isString(expr)) {
143+
throw new DSErrors.IA('"expr" must be a string!');
144+
} else if (!DS.definitions[resourceName]) {
145+
throw new DSErrors.NER(resourceName);
146+
} else if (!DSUtils.isString(id) && !DSUtils.isNumber(id)) {
147+
throw new DSErrors.IA('"id" must be a string or a number!');
148+
}
149+
150+
try {
151+
return scope.$watch(function () {
152+
return _this.lastModified(resourceName, id);
153+
}, function () {
154+
var item = _this.get(resourceName, id);
155+
DSUtils.set(scope, expr, item);
156+
if (cb) {
157+
cb(null, item);
158+
}
159+
});
160+
} catch (err) {
161+
if (cb) {
162+
cb(err);
163+
} else {
164+
throw err;
165+
}
166+
}
167+
};
168+
100169
function load() {
101170
var args = Array.prototype.slice.call(arguments);
102171
var $rootScope = args[args.length - 1];
103172
var store = new JSData.DS(_this.defaults);
104173
var originals = {};
105174

175+
// Register any adapters that have been loaded
106176
for (var i = 0; i < adapters.length; i++) {
107177
if (adapters[i].loaded) {
108178
store.registerAdapter(adapters[i].name, arguments[i]);
109179
}
110180
}
111181

182+
// Wrap certain sync functions with $apply
112183
for (i = 0; i < functionsToWrap.length; i++) {
113184
originals[functionsToWrap[i]] = store[functionsToWrap[i]];
114185
store[functionsToWrap[i]] = (function (name) {
@@ -124,6 +195,17 @@
124195
})(functionsToWrap[i]);
125196
}
126197

198+
// Hook into the digest loop (throttled)
199+
if (typeof Object.observe !== 'function' ||
200+
typeof Array.observe !== 'function') {
201+
$rootScope.$watch(function () {
202+
// Throttle angular-data's digest loop to tenths of a second
203+
return new Date().getTime() / 100 | 0;
204+
}, function () {
205+
store.digest();
206+
});
207+
}
208+
127209
return store;
128210
}
129211

0 commit comments

Comments
 (0)