Skip to content

Commit 143ee58

Browse files
reppnersreppners
authored andcommitted
+ added tests
+ changed type definition to expose definitions as module and as globally defined namespace var
1 parent 4fb28c2 commit 143ee58

File tree

5 files changed

+651
-42
lines changed

5 files changed

+651
-42
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/// <reference path="js-data-angular.d.ts" />
2+
3+
interface IUser {
4+
5+
}
6+
7+
interface CustomScope extends ng.IScope {
8+
9+
comments: Array<any>;
10+
user: IUser;
11+
users: Array<IUser>;
12+
}
13+
14+
angular.module('myApp')
15+
.controller('commentsCtrl', function ($scope:CustomScope, store:JSData_.DS, Comment:JSData_.DSResourceDefinition<any>, User:JSData_.DSResourceDefinition<any>) {
16+
17+
Comment.findAll().then(function (comments) {
18+
$scope.comments = comments;
19+
});
20+
21+
// shortest version
22+
User.bindOne(1, $scope, 'user');
23+
24+
// short version
25+
store.bindOne('user', 1, $scope, 'user');
26+
27+
// long version
28+
$scope.$watch(function () {
29+
return store.lastModified('user', 1);
30+
}, function () {
31+
$scope.user = store.get<IUser>('user', 1);
32+
});
33+
34+
var params = {
35+
where: {
36+
age: {
37+
'>': 30
38+
}
39+
}
40+
};
41+
42+
// shortest verions
43+
User.bindAll(params, $scope, 'users');
44+
45+
// short version
46+
store.bindAll('user', params, $scope, 'users');
47+
48+
// long version
49+
$scope.$watch(function () {
50+
return store.lastModified('user');
51+
}, function () {
52+
$scope.users = store.filter('user', params);
53+
});
54+
});
55+
56+
angular.module('myApp')
57+
.run(function (DS:JSData_.DS) {
58+
// We don't register the "User" resource
59+
// as a service, so it can only be used
60+
// via DS.<method>('user', ...)
61+
// The advantage here is that this code
62+
// is guaranteed to be executed, and you
63+
// only ever have to inject "DS"
64+
DS.defineResource('user');
65+
})
66+
.factory('Comment', function (DS:JSData_.DS) {
67+
// This code won't execute unless you actually
68+
// inject "Comment" somewhere in your code.
69+
// Thanks Angular...
70+
// Some like injecting actual Resource
71+
// definitions, instead of just "DS"
72+
return DS.defineResource('comment');
73+
});
74+
75+
angular.module('myApp')
76+
.config(function (DSProvider:JSData_.DSProvider) {
77+
DSProvider.defaults.basePath = '/myApi'; // etc.
78+
});

js-data-angular/js-data-angular.d.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
/// <reference path="../js-data/js-data.d.ts" />
77
/// <reference path="../angularjs/angular.d.ts" />
88

9-
declare module JSData {
9+
declare module JSData_ {
1010

11-
class ngDS extends DS {
11+
interface DSProvider {
12+
defaults:DSConfiguration;
13+
}
14+
15+
interface DS {
16+
17+
bindAll<T>(resourceName:string, params:DSFilterParams, scope:ng.IScope, expr:string, cb?:(err:DSError, items:Array<T>)=>void):Function;
18+
19+
bindOne<T>(resourceName:string, id:string, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
20+
bindOne<T>(resourceName:string, id:number, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
21+
}
22+
23+
interface DSResourceDefinition<T> {
1224

13-
// sync methods
14-
bindAll<T>(scope:ng.IScope, expr:string, resourceName:string, params:DSFilterParams, cb?:(err:DSError, items:Array<T>)=>void):Function;
25+
bindAll<T>(params:DSFilterParams, scope:ng.IScope, expr:string, cb?:(err:DSError, items:Array<T>)=>void):Function;
1526

16-
bindOne<T>(scope:ng.IScope, expr:string, resourceName:string, id:string, cb?:(err:DSError, item:T)=>void):Function;
17-
bindOne<T>(scope:ng.IScope, expr:string, resourceName:string, id:number, cb?:(err:DSError, item:T)=>void):Function;
27+
bindOne<T>(id:string, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
28+
bindOne<T>(id:number, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
1829
}
1930
}

js-data/js-data-node-tests.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="js-data.d.ts" />
2+
3+
import JSData = require('js-data');
4+
//TODO
5+
//import DSRedisAdapter = require('js-data-redis')
6+
var store = new JSData.DS();
7+
8+
// register and use http by default for async operations
9+
//TODO
10+
//store.registerAdapter('redis', new DSRedisAdapter(), {default: true});
11+
12+
// simplest model definition
13+
var User = store.defineResource('user');
14+
15+
User.find(1).then(function (user: any) {
16+
user; // { id: 1, name: 'John' }
17+
});

0 commit comments

Comments
 (0)