Skip to content

Commit 9b24471

Browse files
committed
Fixes #94.
1 parent 2d79415 commit 9b24471

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
##### 0.10.1 - xx July 2014
22

33
###### Backwards compatible bug fixes
4-
- #90 - DS.create isn't added to completedQueries
5-
- #91 - dist/angular-data(.min).js doesn't end with a semicolon
6-
- #95 - observe-js outdated
4+
- #90 - DS.create isn't added to completedQueries (`DS.create` now adds a completed query entry)
5+
- #91 - dist/angular-data(.min).js doesn't end with a semicolon (upgraded Browserify)
6+
- #94 - Resource object name/class inconsistency (added `useClass` option to `DS.defineResource`)
7+
- #95 - observe-js outdated (Upgraded observe-js.js an refactored to new API)
78

89
##### 0.10.0 - 18 July 2014
910

dist/angular-data.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -3359,6 +3359,7 @@ Defaults.prototype.filter = function (collection, resourceName, params, options)
33593359
};
33603360
Defaults.prototype.baseUrl = '';
33613361
Defaults.prototype.endpoint = '';
3362+
Defaults.prototype.useClass = false;
33623363
/**
33633364
* @doc property
33643365
* @id DSProvider.properties:defaults.beforeValidate
@@ -4147,6 +4148,7 @@ function Resource(utils, options) {
41474148
* - `{string="id"}` - `idAttribute` - The attribute that specifies the primary key for this resource.
41484149
* - `{string=}` - `endpoint` - The attribute that specifies the primary key for this resource. Default is the value of `name`.
41494150
* - `{string=}` - `baseUrl` - The url relative to which all AJAX requests will be made.
4151+
* - `{boolean=}` - `useClass` - Whether to use a wrapper class created from the ProperCase name of the resource. The wrapper will always be used for resources that have `methods` defined.
41504152
* - `{*=}` - `meta` - A property reserved for developer use. This will never be used by the API.
41514153
* - `{object=}` - `methods` - If provided, items of this resource will be wrapped in a constructor function that is
41524154
* empty save for the attributes in this option which will be mixed in to the constructor function prototype. Enabling
@@ -4172,6 +4174,7 @@ function defineResource(definition) {
41724174
var IA = this.errors.IA;
41734175

41744176
if (this.utils.isString(definition)) {
4177+
definition = definition.replace(/\s/gi, '');
41754178
definition = {
41764179
name: definition
41774180
};
@@ -4210,10 +4213,11 @@ function defineResource(definition) {
42104213
storagePrefix: 'DS.' + def.name
42114214
});
42124215

4216+
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
4217+
eval('function ' + def.class + '() {}');
4218+
def[def.class] = eval(def.class);
4219+
42134220
if (def.methods) {
4214-
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
4215-
eval('function ' + def.class + '() {}');
4216-
def[def.class] = eval(def.class);
42174221
this.utils.deepMixIn(def[def.class].prototype, def.methods);
42184222
}
42194223

@@ -4886,7 +4890,7 @@ function _inject(definition, resource, attrs) {
48864890
var item = this.get(definition.name, id);
48874891

48884892
if (!item) {
4889-
if (definition.class) {
4893+
if (definition.methods || definition.useClass) {
48904894
if (attrs instanceof definition[definition.class]) {
48914895
item = attrs;
48924896
} else {

dist/angular-data.min.js

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

src/datastore/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Defaults.prototype.filter = function (collection, resourceName, params, options)
184184
};
185185
Defaults.prototype.baseUrl = '';
186186
Defaults.prototype.endpoint = '';
187+
Defaults.prototype.useClass = false;
187188
/**
188189
* @doc property
189190
* @id DSProvider.properties:defaults.beforeValidate

src/datastore/sync_methods/defineResource.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function Resource(utils, options) {
5050
* - `{string="id"}` - `idAttribute` - The attribute that specifies the primary key for this resource.
5151
* - `{string=}` - `endpoint` - The attribute that specifies the primary key for this resource. Default is the value of `name`.
5252
* - `{string=}` - `baseUrl` - The url relative to which all AJAX requests will be made.
53+
* - `{boolean=}` - `useClass` - Whether to use a wrapper class created from the ProperCase name of the resource. The wrapper will always be used for resources that have `methods` defined.
5354
* - `{*=}` - `meta` - A property reserved for developer use. This will never be used by the API.
5455
* - `{object=}` - `methods` - If provided, items of this resource will be wrapped in a constructor function that is
5556
* empty save for the attributes in this option which will be mixed in to the constructor function prototype. Enabling
@@ -75,6 +76,7 @@ function defineResource(definition) {
7576
var IA = this.errors.IA;
7677

7778
if (this.utils.isString(definition)) {
79+
definition = definition.replace(/\s/gi, '');
7880
definition = {
7981
name: definition
8082
};
@@ -113,10 +115,11 @@ function defineResource(definition) {
113115
storagePrefix: 'DS.' + def.name
114116
});
115117

118+
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
119+
eval('function ' + def.class + '() {}');
120+
def[def.class] = eval(def.class);
121+
116122
if (def.methods) {
117-
def.class = definition.name[0].toUpperCase() + definition.name.substring(1);
118-
eval('function ' + def.class + '() {}');
119-
def[def.class] = eval(def.class);
120123
this.utils.deepMixIn(def[def.class].prototype, def.methods);
121124
}
122125

src/datastore/sync_methods/inject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function _inject(definition, resource, attrs) {
6464
var item = this.get(definition.name, id);
6565

6666
if (!item) {
67-
if (definition.class) {
67+
if (definition.methods || definition.useClass) {
6868
if (attrs instanceof definition[definition.class]) {
6969
item = attrs;
7070
} else {

test/integration/datastore/sync_methods/defineResource.test.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ describe('DS.defineResource(definition)', function () {
4242

4343
assert.doesNotThrow(function () {
4444
DS.defineResource('new resource');
45+
assert.equal(DS.definitions.newresource.class, 'Newresource');
4546
}, 'Should not throw');
4647
});
4748

@@ -95,13 +96,34 @@ describe('DS.defineResource(definition)', function () {
9596
}
9697
});
9798

99+
DS.defineResource({
100+
name: 'dog',
101+
useClass: true
102+
});
103+
104+
DS.defineResource({
105+
name: 'cat'
106+
});
107+
98108
DS.inject('person', {
99109
first: 'John',
100110
last: 'Anderson',
101111
id: 1
102112
});
103113

114+
DS.inject('dog', {
115+
name: 'Spot',
116+
id: 1
117+
});
118+
119+
DS.inject('cat', {
120+
name: 'Sam',
121+
id: 1
122+
});
123+
104124
var user = DS.get('person', 1);
125+
var dog = DS.get('dog', 1);
126+
var cat = DS.get('cat', 1);
105127

106128
assert.deepEqual(JSON.stringify(user), JSON.stringify({
107129
first: 'John',
@@ -110,10 +132,12 @@ describe('DS.defineResource(definition)', function () {
110132
}));
111133
assert.equal(user.fullName(), 'John Anderson');
112134
assert.isTrue(user instanceof DS.definitions.person[DS.definitions.person.class]);
135+
assert.isTrue(dog instanceof DS.definitions.dog[DS.definitions.dog.class]);
136+
assert.isTrue(cat instanceof Object);
113137
assert.equal(DS.definitions.person.class, 'Person');
114138
assert.equal(DS.definitions.person[DS.definitions.person.class].name, 'Person');
115-
assert.equal(lifecycle.beforeInject.callCount, 1, 'beforeInject should have been called');
116-
assert.equal(lifecycle.afterInject.callCount, 1, 'afterInject should have been called');
139+
assert.equal(lifecycle.beforeInject.callCount, 3, 'beforeInject should have been called');
140+
assert.equal(lifecycle.afterInject.callCount, 3, 'afterInject should have been called');
117141
});
118142
it('should allow definition of computed properties', function (done) {
119143
var callCount = 0;

0 commit comments

Comments
 (0)