+ added tests

+ changed type definition to expose definitions as module and as globally defined namespace var
This commit is contained in:
reppners
2015-02-26 09:53:11 +01:00
parent 4fb28c2c4a
commit 143ee58d48
5 changed files with 652 additions and 43 deletions
+78
View File
@@ -0,0 +1,78 @@
/// <reference path="js-data-angular.d.ts" />
interface IUser {
}
interface CustomScope extends ng.IScope {
comments: Array<any>;
user: IUser;
users: Array<IUser>;
}
angular.module('myApp')
.controller('commentsCtrl', function ($scope:CustomScope, store:JSData_.DS, Comment:JSData_.DSResourceDefinition<any>, User:JSData_.DSResourceDefinition<any>) {
Comment.findAll().then(function (comments) {
$scope.comments = comments;
});
// shortest version
User.bindOne(1, $scope, 'user');
// short version
store.bindOne('user', 1, $scope, 'user');
// long version
$scope.$watch(function () {
return store.lastModified('user', 1);
}, function () {
$scope.user = store.get<IUser>('user', 1);
});
var params = {
where: {
age: {
'>': 30
}
}
};
// shortest verions
User.bindAll(params, $scope, 'users');
// short version
store.bindAll('user', params, $scope, 'users');
// long version
$scope.$watch(function () {
return store.lastModified('user');
}, function () {
$scope.users = store.filter('user', params);
});
});
angular.module('myApp')
.run(function (DS:JSData_.DS) {
// We don't register the "User" resource
// as a service, so it can only be used
// via DS.<method>('user', ...)
// The advantage here is that this code
// is guaranteed to be executed, and you
// only ever have to inject "DS"
DS.defineResource('user');
})
.factory('Comment', function (DS:JSData_.DS) {
// This code won't execute unless you actually
// inject "Comment" somewhere in your code.
// Thanks Angular...
// Some like injecting actual Resource
// definitions, instead of just "DS"
return DS.defineResource('comment');
});
angular.module('myApp')
.config(function (DSProvider:JSData_.DSProvider) {
DSProvider.defaults.basePath = '/myApi'; // etc.
});
+17 -6
View File
@@ -6,14 +6,25 @@
/// <reference path="../js-data/js-data.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
declare module JSData {
declare module JSData_ {
class ngDS extends DS {
interface DSProvider {
defaults:DSConfiguration;
}
// sync methods
bindAll<T>(scope:ng.IScope, expr:string, resourceName:string, params:DSFilterParams, cb?:(err:DSError, items:Array<T>)=>void):Function;
interface DS {
bindOne<T>(scope:ng.IScope, expr:string, resourceName:string, id:string, cb?:(err:DSError, item:T)=>void):Function;
bindOne<T>(scope:ng.IScope, expr:string, resourceName:string, id:number, cb?:(err:DSError, item:T)=>void):Function;
bindAll<T>(resourceName:string, params:DSFilterParams, scope:ng.IScope, expr:string, cb?:(err:DSError, items:Array<T>)=>void):Function;
bindOne<T>(resourceName:string, id:string, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
bindOne<T>(resourceName:string, id:number, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
}
interface DSResourceDefinition<T> {
bindAll<T>(params:DSFilterParams, scope:ng.IScope, expr:string, cb?:(err:DSError, items:Array<T>)=>void):Function;
bindOne<T>(id:string, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
bindOne<T>(id:number, scope:ng.IScope, expr:string, cb?:(err:DSError, item:T)=>void):Function;
}
}