Skip to content

Commit

Permalink
Constructor functions for adding custom behavior are now named functi…
Browse files Browse the repository at this point in the history
…ons.
  • Loading branch information
jmdobry committed May 15, 2014
1 parent 4e85954 commit 09217a6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
10 changes: 6 additions & 4 deletions dist/angular-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,7 @@ function changes(resourceName, id) {
module.exports = changes;

},{}],44:[function(require,module,exports){
/*jshint evil:true*/
var errorPrefix = 'DS.defineResource(definition): ';

function Resource(utils, options) {
Expand Down Expand Up @@ -2955,9 +2956,10 @@ function defineResource(definition) {
});

if (def.methods) {
def.factory = function () {
};
this.utils.deepMixIn(def.factory.prototype, def.methods);
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
eval('function ' + def.class + '() {}');
def[def.class] = eval(def.class);
this.utils.deepMixIn(def[def.class].prototype, def.methods);
}

this.store[def.name] = {
Expand Down Expand Up @@ -3672,7 +3674,7 @@ function _inject(definition, resource, attrs) {
item = this.get(definition.name, id);

if (!item) {
item = definition.factory ? new definition.factory() : {};
item = definition.class ? new definition[definition.class]() : {};
resource.previousAttributes[id] = {};

_this.utils.deepMixIn(item, attrs);
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-data.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/datastore/sync_methods/defineResource.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*jshint evil:true*/
var errorPrefix = 'DS.defineResource(definition): ';

function Resource(utils, options) {
Expand Down Expand Up @@ -107,9 +108,10 @@ function defineResource(definition) {
});

if (def.methods) {
def.factory = function () {
};
this.utils.deepMixIn(def.factory.prototype, def.methods);
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
eval('function ' + def.class + '() {}');
def[def.class] = eval(def.class);
this.utils.deepMixIn(def[def.class].prototype, def.methods);
}

this.store[def.name] = {
Expand Down
2 changes: 1 addition & 1 deletion src/datastore/sync_methods/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function _inject(definition, resource, attrs) {
item = this.get(definition.name, id);

if (!item) {
item = definition.factory ? new definition.factory() : {};
item = definition.class ? new definition[definition.class]() : {};
resource.previousAttributes[id] = {};

_this.utils.deepMixIn(item, attrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ describe('DS.defineResource(definition)', function () {
id: 1
}));
assert.equal(user.fullName(), 'John Anderson');
assert.isTrue(user instanceof DS.definitions.user.factory);
assert.isTrue(user instanceof DS.definitions.user[DS.definitions.user.class]);
assert.equal(DS.definitions.user.class, 'User');
assert.equal(DS.definitions.user[DS.definitions.user.class].name, 'User');
assert.equal(lifecycle.beforeInject.callCount, 1, 'beforeInject should have been called');
assert.equal(lifecycle.afterInject.callCount, 1, 'afterInject should have been called');
});
Expand Down

2 comments on commit 09217a6

@jmdobry
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kentcdodds it worked!

@kentcdodds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I see no problem with this: eval('function ' + def.class + '() {}'); it doesn't even make me squirm much :) Haha. Looks good to me. Thanks!

Please sign in to comment.