Skip to content

Commit 8895040

Browse files
committed
Various improvements.
1 parent 5ff48a6 commit 8895040

File tree

14 files changed

+137
-24
lines changed

14 files changed

+137
-24
lines changed

Gruntfile.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ module.exports = function (grunt) {
239239
storage: 5
240240
}
241241
},
242+
{
243+
id: 'angular-data-mocks',
244+
title: 'angular-data-mocks',
245+
docs: ['guide/angular-data-mocks/'],
246+
rank: {
247+
index: 1,
248+
overview: 2,
249+
setup: 3,
250+
testing: 4
251+
}
252+
},
242253
{
243254
id: 'angular-data-resource',
244255
title: 'Defining Resources',
@@ -248,7 +259,8 @@ module.exports = function (grunt) {
248259
overview: 2,
249260
basic: 3,
250261
advanced: 4,
251-
lifecycle: 5
262+
lifecycle: 5,
263+
custom: 6
252264
}
253265
},
254266
{

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
"devDependencies": {
2929
"angular": "~1.2.16",
3030
"angular-mocks": "~1.2.16",
31-
"angular-data-mocks": "0.2.0",
3231
"angular-cache": "~3.0.0-beta.4",
33-
"observe-js": "~0.2.0"
32+
"observe-js": "~0.2.0",
33+
"angular-data-mocks": "~0.3.1"
3434
}
3535
}

dist/angular-data.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -3956,7 +3956,15 @@ function _inject(definition, resource, attrs) {
39563956
item = this.get(definition.name, id);
39573957

39583958
if (!item) {
3959-
item = definition.class ? new definition[definition.class]() : {};
3959+
if (definition.class) {
3960+
if (attrs instanceof definition[definition.class]) {
3961+
item = attrs;
3962+
} else {
3963+
item = new definition[definition.class]();
3964+
}
3965+
} else {
3966+
item = {};
3967+
}
39603968
resource.previousAttributes[id] = {};
39613969

39623970
_this.utils.deepMixIn(item, attrs);

dist/angular-data.min.js

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

guide/angular-data-mocks/index.doc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@doc overview
2+
@id index
3+
@name Overview
4+
@description
5+
6+
# Overview
7+
8+
<page-list></page-list>

guide/angular-data-mocks/overview.doc

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@doc overview
2+
@id overview
3+
@name Overview
4+
@description
5+
6+
Angular-data is a fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.
7+
8+
__Version:__ 0.3.0
9+
10+
__angular-data-mocks requires [sinon](http://sinonjs.org/) to be loaded in order to work.__
11+
12+
Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.

guide/angular-data-mocks/setup.doc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@doc overview
2+
@id setup
3+
@name Setting Up
4+
@description
5+
6+
TODO: Explain how to set up angular tests to use angular-data-mocks.
7+
8+
Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.

guide/angular-data-mocks/testing.doc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@doc overview
2+
@id testing
3+
@name Testing
4+
@description
5+
6+
TODO: Explain how to use angular-data-mocks in angular tests.
7+
8+
Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.

guide/angular-data.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
body {
22
background-size: auto;
33
background: url(/resources/img/cream_dust.png) repeat repeat;
4-
font-family:'Open sans',Arial,Helvetica,sans-serif;
4+
font-family: 'Open sans', Arial, Helvetica, sans-serif;
55
}
66

77
.main-nav {
88
border-bottom: 2px solid #0088cc;
99
}
1010

11+
.main-nav .dropdown-menu {
12+
min-width: 250px;
13+
}
14+
1115
.main-nav:before {
1216
content: " ";
1317
position: absolute;

guide/angular-data/resource/resource.doc

+30
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,33 @@ angular.module('myApp', ['angular-data.DS'])
167167

168168
});
169169
```
170+
171+
@doc overview
172+
@id custom
173+
@name Custom Model Behavior
174+
@description
175+
176+
If you provide a `methods` field in the options passed to `DS.defineResource`, angular-data wrap items of that resource
177+
with an empty constructor function. The `methods` option should be an object where the keys are method names and the
178+
values are functions. This object will be mixed in to the prototype empty constructor function used to wrap items of the
179+
new resource. In this way you can add custom behavior to what will now be "instances" of the new resource.
180+
181+
## Example:
182+
```js
183+
DS.defineResource({
184+
name: 'user',
185+
methods: {
186+
fullName: function () {
187+
return this.first + ' ' + this.last;
188+
}
189+
}
190+
});
191+
192+
DS.inject('user', { id: 1, first: 'John', last: 'Anderson' });
193+
194+
var user = DS.get('user', 1);
195+
196+
user.fullName(); // "John Anderson"
197+
198+
user.constructor; // function User() { ... }
199+
```

guide/angular-data/resources.doc

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ myApp.run(function (DS) {
1818
});
1919
```
2020

21-
See the [Resource Guide](/documentation/guide/resource/index) for detailed information on defining resources.
21+
See the [Resource Guide](/documentation/guide/angular-data-resource/index) for detailed information on defining resources.

guide/angular-data/synchronous.doc

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ $scope.$watch(function () {
2525
// When this callback is executed, it means that the data store thinks the item changed
2626

2727
// Retrieve the updated item from the data store's cache
28-
$scope.document = DS.get('document', 45);
28+
$scope.myDoc = DS.get('document', 45);
2929
});
3030
```
3131

32+
To make things simpler, angular-data has some bind methods to help with this:
33+
34+
```js
35+
DS.bindOne($scope, 'myDoc', 'document', 45');
36+
```
37+
38+
The above example shows how to bind an item in the data store to the stop. Whenever that item changes it will be updated
39+
on the $scope.
40+
3241
When the app starts up, the calls to `lastModified()` and `get()` will both returned undefined, because the item isn't in
3342
the data store yet. If we insert the statement: `DS.find('document', 45);` right above the `$watch` function, the data store will make an
3443
AJAX request for that item. When the item returns from the server, the last modified timestamp for that item will change

guide/nav.html

+21-15
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<a href="/documentation/guide/angular-data/overview">Basics</a>
2727
</li>
2828
<li>
29-
<a href="/documentation/guide/angular-data/resources">Defining Resources</a>
29+
<a href="/documentation/guide/angular-data-resource/basic">Defining Resources</a>
3030
</li>
3131
<li>
3232
<a href="/documentation/guide/angular-data/synchronous">Synchronous Methods</a>
@@ -35,6 +35,11 @@
3535
<a href="/documentation/guide/angular-data/asynchronous">Asynchronous Methods</a>
3636
</li>
3737
<li class="divider"></li>
38+
<li class="nav-header">Angular-data-mocks</li>
39+
<li>
40+
<a href="/documentation/guide/angular-data-mocks/overview">Testing Guide</a>
41+
</li>
42+
<li class="divider"></li>
3843
<li class="nav-header">Angular-cache</li>
3944
<li>
4045
<a href="/documentation/guide/angular-cache/index">Overview</a>
@@ -59,9 +64,6 @@
5964
</a>
6065
<ul class="dropdown-menu">
6166
<li class="nav-header">Angular-data - 0.9.0</li>
62-
<li>
63-
<a href="/documentation/api/angular-data">API Index</a>
64-
</li>
6567
<li>
6668
<a href="/documentation/api/angular-data/angular-data">Overview</a>
6769
</li>
@@ -71,32 +73,28 @@
7173
<li>
7274
<a href="/documentation/api/angular-data/DSHttpAdapter">DSHttpAdapter</a>
7375
</li>
76+
<li class="divider"></li>
77+
<li class="nav-header">Angular-data-mocks - 0.3.0</li>
7478
<li>
75-
<a href="/documentation/api/angular-data/DSUtils">DSUtils</a>
79+
<a href="/documentation/api/angular-data-mocks/angular-data-mocks">Overview</a>
7680
</li>
7781
<li>
78-
<a href="/documentation/api/angular-data/DSErrors">DSErrors</a>
82+
<a href="/documentation/api/angular-data-mocks/DS">DS</a>
7983
</li>
80-
<li class="divider"></li>
81-
<li class="nav-header">Angular-cache - 3.0.0-beta.4</li>
8284
<li>
83-
<a href="/documentation/api/angular-cache">API Index</a>
85+
<a href="/documentation/api/angular-data-mocks/DSHttpAdapter">DSHttpAdapter</a>
8486
</li>
87+
<li class="divider"></li>
88+
<li class="nav-header">Angular-cache - 3.0.0-beta.4</li>
8589
<li>
8690
<a href="/documentation/api/angular-cache/angular-cache">Overview</a>
8791
</li>
88-
<li>
89-
<a href="/documentation/api/angular-cache/DSCacheFactoryProvider">DSCacheFactoryProvider</a>
90-
</li>
9192
<li>
9293
<a href="/documentation/api/angular-cache/DSCacheFactory">DSCacheFactory</a>
9394
</li>
9495
<li>
9596
<a href="/documentation/api/angular-cache/DSCache">DSCache</a>
9697
</li>
97-
<li>
98-
<a href="/documentation/api/angular-cache/DSBinaryHeap">DSBinaryHeap</a>
99-
</li>
10098
</ul>
10199
</li>
102100
<li class="dropdown">
@@ -115,6 +113,14 @@
115113
<a href="https://github.com/jmdobry/angular-data">GitHub</a>
116114
</li>
117115
<li class="divider"></li>
116+
<li class="nav-header">Angular-data-mocks</li>
117+
<li>
118+
<a href="https://github.com/jmdobry/angular-data-mocks/issues">Issues</a>
119+
</li>
120+
<li>
121+
<a href="https://github.com/jmdobry/angular-data-mocks">GitHub</a>
122+
</li>
123+
<li class="divider"></li>
118124
<li class="nav-header">Angular-cache</li>
119125
<li>
120126
<a href="https://groups.google.com/forum/?fromgroups#!forum/angular-cache">Mailing List</a>

src/datastore/sync_methods/inject.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ function _inject(definition, resource, attrs) {
3434
item = this.get(definition.name, id);
3535

3636
if (!item) {
37-
item = definition.class ? new definition[definition.class]() : {};
37+
if (definition.class) {
38+
if (attrs instanceof definition[definition.class]) {
39+
item = attrs;
40+
} else {
41+
item = new definition[definition.class]();
42+
}
43+
} else {
44+
item = {};
45+
}
3846
resource.previousAttributes[id] = {};
3947

4048
_this.utils.deepMixIn(item, attrs);

0 commit comments

Comments
 (0)