8000 Merge pull request #147 from js-data/feat-aop · StudyForFun/js-data@15884f2 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 15884f2

Browse files
committed
Merge pull request js-data#147 from js-data/feat-aop
feat-aop
2 parents 49e380c + 4c6c0be commit 15884f2

File tree

5 files changed

+85
-3
lines changed

5 files changed

+85
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
##### 2.0.0-beta.6 - xx June 2015
2+
3+
###### Breaking API changes
4+
- #150 - Debug output, `debug` now defaults to `false`
5+
6+
###### Backwards compatible API changes
7+
- #145 - A little AOP, add a `.before` to all methods, allowing per-method argument customization
8+
19
##### 2.0.0-beta.5 - 27 May 2015
210

311
###### Breaking API changes

mocha.start.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@ beforeEach(function () {
337337
globals.store = store;
338338
global.store = globals.store;
339339

340+
globals.JSData = JSData;
341+
global.JSData = globals.JSData;
342+
340343
globals.DSUtils = DSUtils;
341344
global.DSUtils = globals.DSUtils;
342345

src/datastore/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defaultsPrototype.cacheResponse = !!DSUtils.w;
9393
defaultsPrototype.clearEmptyQueries = true;
9494
defaultsPrototype.computed = {};
9595
defaultsPrototype.defaultAdapter = 'http';
96-
defaultsPrototype.debug = true;
96+
defaultsPrototype.debug = false;
9797
defaultsPrototype.defaultValues = {};
9898
defaultsPrototype.eagerEject = false;
9999
// TODO: Implement eagerInject in DS#create
@@ -372,7 +372,20 @@ dsPrototype.getAdapter.shorthand = false;
372372
dsPrototype.registerAdapter.shorthand = false;
373373
dsPrototype.errors = DSErrors;
374374
dsPrototype.utils = DSUtils;
375-
DSUtils.deepMixIn(dsPrototype, syncMethods);
376-
DSUtils.deepMixIn(dsPrototype, asyncMethods);
375+
376+
function addMethods(target, obj) {
377+
DSUtils.forOwn(obj, (v, k) => {
378+
target[k] = v;
379+
target[k].before = function (fn) {
380+
let orig = target[k];
381+
target[k] = function (...args) {
382+
return orig.apply(this, fn.apply(this, args) || args);
383+
};
384+
};
385+
});
386+
}
387+
388+
addMethods(dsPrototype, syncMethods);
389+
addMethods(dsPrototype, asyncMethods);
377390

378391
export default DS;

src/datastore/sync_methods/defineResource.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ export default function defineResource(definition) {
277277
args.unshift(def.n);
278278
return _this[k].apply(_this, args);
279279
};
280+
def[k].before = fn => {
281+
let orig = def[k];
282+
def[k] = (...args) => {
283+
return orig.apply(def, fn.apply(def, args) || args);
284+
};
285+
};
280286
} else {
281287
def[k] = (...args) => _this[k].apply(_this, args);
282288
}

test/both/datastore/sync_methods/defineResource.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,56 @@ describe('DS#defineResource', function () {
244244
bazId: 10
245245
}));
246246
});
247+
it('should allow a bit of aspect oriented programming', function () {
248+
var Foo = store.defineResource('foo');
249+
250+
var orig = Foo.createInstance;
251+
Foo.createInstance.before(function (attrs) {
252+
assert.isTrue(this === Foo);
253+
if (attrs && !('name' in attrs)) {
254+
attrs.name = 'hi';
255+
} else if (arguments.length === 0) {
256+
return [{ id: 'anonymous' }];
257+
}
258+
});
259+
assert.isFalse(orig === Foo.createInstance);
260+
261+
var foo = Foo.createInstance({ id: 1 });
262+
var foo2 = Foo.createInstance();
263+
264+
assert.deepEqual(DSUtils.toJson(foo), DSUtils.toJson({ id: 1, name: 'hi' }));
265+
assert.deepEqual(DSUtils.toJson(foo2), DSUtils.toJson({ id: 'anonymous' }));
266+
267+
var newStore = new JSData.DS({ log: false });
268+
orig = newStore.createInstance;
269+
newStore.createInstance.before(function (resourceName, attrs) {
270+
if (attrs) {
271+
attrs.foo = 'bar';
272+
}
273+
assert.isTrue(this === newStore);
274+
});
275+
assert.isFalse(orig === newStore.createInstance);
276+
277+
var NewFoo = newStore.defineResource('newFoo');
278+
279+
foo = newStore.createInstance('newFoo', { id: 1 });
280+
281+
assert.equal(foo.id, 1);
282+
assert.equal(foo.foo, 'bar');
283+
284+
NewFoo.createInstance.before(function (attrs) {
285+
if (attrs) {
286+
attrs.beep = 'boop';
287+
}
288+
assert.isTrue(this === NewFoo);
289+
});
290+
foo = NewFoo.createInstance({ id: 1 });
291+
292+
assert.equal(foo.id, 1);
293+
assert.equal(foo.beep, 'boop');
294+
assert.equal(foo.foo, 'bar');
295+
296+
// clean up
297+
newStore.constructor.prototype.createInstance = orig;
298+
});
247299
});

0 commit comments

Comments
 (0)
0