Skip to content

Commit

Permalink
Various improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed May 16, 2014
1 parent 5ff48a6 commit 8895040
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 24 deletions.
14 changes: 13 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ module.exports = function (grunt) {
storage: 5
}
},
{
id: 'angular-data-mocks',
title: 'angular-data-mocks',
docs: ['guide/angular-data-mocks/'],
rank: {
index: 1,
overview: 2,
setup: 3,
testing: 4
}
},
{
id: 'angular-data-resource',
title: 'Defining Resources',
Expand All @@ -248,7 +259,8 @@ module.exports = function (grunt) {
overview: 2,
basic: 3,
advanced: 4,
lifecycle: 5
lifecycle: 5,
custom: 6
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"devDependencies": {
"angular": "~1.2.16",
"angular-mocks": "~1.2.16",
"angular-data-mocks": "0.2.0",
"angular-cache": "~3.0.0-beta.4",
"observe-js": "~0.2.0"
"observe-js": "~0.2.0",
"angular-data-mocks": "~0.3.1"
}
}
10 changes: 9 additions & 1 deletion dist/angular-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3956,7 +3956,15 @@ function _inject(definition, resource, attrs) {
item = this.get(definition.name, id);

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

_this.utils.deepMixIn(item, attrs);
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-data.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions guide/angular-data-mocks/index.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@doc overview
@id index
@name Overview
@description

# Overview

<page-list></page-list>
12 changes: 12 additions & 0 deletions guide/angular-data-mocks/overview.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@doc overview
@id overview
@name Overview
@description

Angular-data is a fake angular-data implementation suitable for unit testing angular applications that use the `angular-data.DS` module.

__Version:__ 0.3.0

__angular-data-mocks requires [sinon](http://sinonjs.org/) to be loaded in order to work.__

Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.
8 changes: 8 additions & 0 deletions guide/angular-data-mocks/setup.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@doc overview
@id setup
@name Setting Up
@description

TODO: Explain how to set up angular tests to use angular-data-mocks.

Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.
8 changes: 8 additions & 0 deletions guide/angular-data-mocks/testing.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@doc overview
@id testing
@name Testing
@description

TODO: Explain how to use angular-data-mocks in angular tests.

Refer to the [angular-data-mocks API](/documentation/api/angular-data-mocks/angular-data-mocks) for more detailed information.
6 changes: 5 additions & 1 deletion guide/angular-data.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
body {
background-size: auto;
background: url(/resources/img/cream_dust.png) repeat repeat;
font-family:'Open sans',Arial,Helvetica,sans-serif;
font-family: 'Open sans', Arial, Helvetica, sans-serif;
}

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

.main-nav .dropdown-menu {
min-width: 250px;
}

.main-nav:before {
content: " ";
position: absolute;
Expand Down
30 changes: 30 additions & 0 deletions guide/angular-data/resource/resource.doc
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,33 @@ angular.module('myApp', ['angular-data.DS'])

});
```

@doc overview
@id custom
@name Custom Model Behavior
@description

If you provide a `methods` field in the options passed to `DS.defineResource`, angular-data wrap items of that resource
with an empty constructor function. The `methods` option should be an object where the keys are method names and the
values are functions. This object will be mixed in to the prototype empty constructor function used to wrap items of the
new resource. In this way you can add custom behavior to what will now be "instances" of the new resource.

## Example:
```js
DS.defineResource({
name: 'user',
methods: {
fullName: function () {
return this.first + ' ' + this.last;
}
}
});

DS.inject('user', { id: 1, first: 'John', last: 'Anderson' });

var user = DS.get('user', 1);

user.fullName(); // "John Anderson"

user.constructor; // function User() { ... }
```
2 changes: 1 addition & 1 deletion guide/angular-data/resources.doc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ myApp.run(function (DS) {
});
```

See the [Resource Guide](/documentation/guide/resource/index) for detailed information on defining resources.
See the [Resource Guide](/documentation/guide/angular-data-resource/index) for detailed information on defining resources.
11 changes: 10 additions & 1 deletion guide/angular-data/synchronous.doc
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ $scope.$watch(function () {
// When this callback is executed, it means that the data store thinks the item changed

// Retrieve the updated item from the data store's cache
$scope.document = DS.get('document', 45);
$scope.myDoc = DS.get('document', 45);
});
```

To make things simpler, angular-data has some bind methods to help with this:

```js
DS.bindOne($scope, 'myDoc', 'document', 45');
```

The above example shows how to bind an item in the data store to the stop. Whenever that item changes it will be updated
on the $scope.

When the app starts up, the calls to `lastModified()` and `get()` will both returned undefined, because the item isn't in
the data store yet. If we insert the statement: `DS.find('document', 45);` right above the `$watch` function, the data store will make an
AJAX request for that item. When the item returns from the server, the last modified timestamp for that item will change
Expand Down
36 changes: 21 additions & 15 deletions guide/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<a href="/documentation/guide/angular-data/overview">Basics</a>
</li>
<li>
<a href="/documentation/guide/angular-data/resources">Defining Resources</a>
<a href="/documentation/guide/angular-data-resource/basic">Defining Resources</a>
</li>
<li>
<a href="/documentation/guide/angular-data/synchronous">Synchronous Methods</a>
Expand All @@ -35,6 +35,11 @@
<a href="/documentation/guide/angular-data/asynchronous">Asynchronous Methods</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-data-mocks</li>
<li>
<a href="/documentation/guide/angular-data-mocks/overview">Testing Guide</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-cache</li>
<li>
<a href="/documentation/guide/angular-cache/index">Overview</a>
Expand All @@ -59,9 +64,6 @@
</a>
<ul class="dropdown-menu">
<li class="nav-header">Angular-data - 0.9.0</li>
<li>
<a href="/documentation/api/angular-data">API Index</a>
</li>
<li>
<a href="/documentation/api/angular-data/angular-data">Overview</a>
</li>
Expand All @@ -71,32 +73,28 @@
<li>
<a href="/documentation/api/angular-data/DSHttpAdapter">DSHttpAdapter</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-data-mocks - 0.3.0</li>
<li>
<a href="/documentation/api/angular-data/DSUtils">DSUtils</a>
<a href="/documentation/api/angular-data-mocks/angular-data-mocks">Overview</a>
</li>
<li>
<a href="/documentation/api/angular-data/DSErrors">DSErrors</a>
<a href="/documentation/api/angular-data-mocks/DS">DS</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-cache - 3.0.0-beta.4</li>
<li>
<a href="/documentation/api/angular-cache">API Index</a>
<a href="/documentation/api/angular-data-mocks/DSHttpAdapter">DSHttpAdapter</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-cache - 3.0.0-beta.4</li>
<li>
<a href="/documentation/api/angular-cache/angular-cache">Overview</a>
</li>
<li>
<a href="/documentation/api/angular-cache/DSCacheFactoryProvider">DSCacheFactoryProvider</a>
</li>
<li>
<a href="/documentation/api/angular-cache/DSCacheFactory">DSCacheFactory</a>
</li>
<li>
<a href="/documentation/api/angular-cache/DSCache">DSCache</a>
</li>
<li>
<a href="/documentation/api/angular-cache/DSBinaryHeap">DSBinaryHeap</a>
</li>
</ul>
</li>
<li class="dropdown">
Expand All @@ -115,6 +113,14 @@
<a href="https://github.com/jmdobry/angular-data">GitHub</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-data-mocks</li>
<li>
<a href="https://github.com/jmdobry/angular-data-mocks/issues">Issues</a>
</li>
<li>
<a href="https://github.com/jmdobry/angular-data-mocks">GitHub</a>
</li>
<li class="divider"></li>
<li class="nav-header">Angular-cache</li>
<li>
<a href="https://groups.google.com/forum/?fromgroups#!forum/angular-cache">Mailing List</a>
Expand Down
10 changes: 9 additions & 1 deletion src/datastore/sync_methods/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ function _inject(definition, resource, attrs) {
item = this.get(definition.name, id);

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

_this.utils.deepMixIn(item, attrs);
Expand Down

0 comments on commit 8895040

Please sign in to comment.