Add Restangular definitions

This commit is contained in:
Boris Yankov
2013-05-25 16:13:42 +03:00
parent edc22399e2
commit 32d658d5f7
3 changed files with 177 additions and 0 deletions
+1
View File
@@ -138,6 +138,7 @@ List of Definitions
* [Restify](https://github.com/mcavage/node-restify) (by [Bret Little](https://github.com/blittle))
* [Rx.js](http://rx.codeplex.com/) (by [gsino](http://www.codeplex.com/site/users/view/gsino))
* [Raphael](http://raphaeljs.com/) (by [CheCoxshall](https://github.com/CheCoxshall))
* [Restangular](https://github.com/mgonto/restangular/) (by [Boris Yankov](https://github.com/borisyankov))
* [require.js](http://requirejs.org/) (by [Josh Baldwin](https://github.com/jbaldwin/))
* [Sammy.js](http://sammyjs.org/) (by [Boris Yankov](https://github.com/borisyankov))
* [Select2](http://ivaynberg.github.com/select2/) (by [Boris Yankov](https://github.com/borisyankov))
+107
View File
@@ -0,0 +1,107 @@
/// <reference path="restangular.d.ts" />
function test_basic() {
var $scope;
Restangular.all('accounts');
Restangular.one('accounts', 1234);
Restangular.one('users').getList().then(function (users) {
$scope.user = users[0];
})
$scope.cars = $scope.user.getList('cars');
$scope.user.sendMessage();
$scope.user.one('message', 123).all('unread').getList();
var baseAccounts = Restangular.all('accounts');
$scope.allAccounts = baseAccounts.getList();
var newAccount = { name: "Gonto's account" };
baseAccounts.post(newAccount);
Restangular.one('accounts', 123).one('buildings', 456).get()
Restangular.one('accounts', 123).all('buildings').getList()
baseAccounts.getList().then(function (accounts) {
var firstAccount = accounts[0];
$scope.buildings = firstAccount.getList("buildings");
$scope.loggedInPlaces = firstAccount.getList("places", { query: 'wuut' }, { 'x-user': 'mgonto' })
firstAccount.name = "Gonto"
var editFirstAccount = Restangular.copy(firstAccount);
firstAccount.put();
editFirstAccount.put();
firstAccount.remove();
var myBuilding = {
name: "Gonto's Building",
place: "Argentina"
};
firstAccount.post("Buildings", myBuilding).then(function () {
console.log("Object saved OK");
}, function () {
console.log("There was an error saving");
});
firstAccount.getList("users", { query: 'wuut' }).then(function (users) {
users.post({ userName: 'unknown' });
users.customGET("messages", { param: "myParam" })
var firstUser = users[0];
$scope.userFromServer = firstUser.get();
firstUser.head()
});
}, function errorCallback() {
alert("Oops error from server :(");
})
var account = Restangular.one("accounts", 123);
$scope.account = account.get({ single: true });
account.customPOST("messages", { param: "myParam" }, {}, { name: "My Message" })
}
function test_config() {
RestangularProvider.setBaseUrl('/api/v1');
RestangularProvider.setExtraFields(['name']);
RestangularProvider.setResponseExtractor(function (response, operation) {
return response.data;
});
RestangularProvider.setDefaultHttpFields({ cache: true });
RestangularProvider.setMethodOverriders(["put", "patch"]);
RestangularProvider.setListTypeIsArray(true);
RestangularProvider.setRestangularFields({
id: "_id",
route: "restangularRoute"
});
RestangularProvider.setRequestSuffix('.json');
RestangularProvider.setRequestInterceptor(function (element, operation, route, url) {
});
RestangularProvider.addElementTransformer('accounts', false, function (elem) {
elem.accountName = 'Changed';
return elem;
});
}
+69
View File
@@ -0,0 +1,69 @@
// Type definitions for Restangular 0.6.9
// Project: https://github.com/mgonto/restangular
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../angularjs/angular.d.ts" />
interface Restangular extends RestangularCustom {
one(route: string, id?: number): RestangularElement;
all(route: string): RestangularCollection;
copy(fromElement: any): RestangularElement;
}
interface RestangularElement extends Restangular {
get (): ng.IPromise;
get (params: any): ng.IPromise;
get (params: any, headers: any): ng.IPromise;
getList(): ng.IPromise;
getList(subElement: any): ng.IPromise;
getList(subElement: any, queryParams: string, headers: any): ng.IPromise;
put(queryParams?: string, headers?: any): ng.IPromise;
post(subElement, elementToPost, queryParams?, headers?): ng.IPromise;
remove(queryParams?, headers?): ng.IPromise;
head(queryParams?, headers?): ng.IPromise;
trace(queryParams?, headers?): ng.IPromise;
options(queryParams?, headers?): ng.IPromise;
patch(queryParams?, headers?): ng.IPromise;
}
interface RestangularCollection extends Restangular {
getList(queryParams?, headers?): ng.IPromise;
post(elementToPost, queryParams?, headers?): ng.IPromise;
head(queryParams?, headers?): ng.IPromise;
trace(queryParams?, headers?): ng.IPromise;
options(queryParams?, headers?): ng.IPromise;
patch(queryParams?, headers?): ng.IPromise;
putElement(idx, params, headers): ng.IPromise;
}
interface RestangularCustom {
customGET(path, params?, headers?): ng.IPromise;
customGETLIST(path, params?, headers?): ng.IPromise;
customDELETE(path, params?, headers?): ng.IPromise;
customPOST(path, params?, headers?, elem?): ng.IPromise;
customPUT(path, params?, headers?, elem?): ng.IPromise;
customOperation(operation, path, params?, headers?, elem?): ng.IPromise;
addRestangularMethod(name, operation, path?, params?, headers?, elem?): ng.IPromise;
}
interface RestangularProvider {
setBaseUrl(newValue: string): void;
setExtraFields(newValues: string[]): void;
setDefaultHttpFields(newValue: any): void;
setMethodOverriders(newValue: any): void;
setResponseExtractor(newValue: any): void;
setRequestInterceptor(newValue: any): void;
setListTypeIsArray(newValue: bool): void;
setRestangularFields(newValue: any): void;
setRequestSuffix(newValue: any): void;
addElementTransformer(type, secondArg, thirdArg): void;
}
declare var Restangular: Restangular;
declare var RestangularProvider: RestangularProvider;