使用
创建主 Restangular 对象
There are 3 ways of creating a main Restangular object. The first one and most common one is by stating the main route of all requests. The second one is by stating the main route and object of all requests.
// 只声明主路由
Restangular.all('accounts')
// 声明主对象
Restangular.one('accounts', 1234)
// 获取这些账户的列表
Restangular.several('accounts', 1234, 123, 12345);
编码
好了已经有主对象了,让我们玩她吧。
// 第一步创建一个 Restangular 对象. 确定基础URL
var baseAccounts = Restangular.all('accounts');
// 这将查询 /accounts 并返回一个诺言.
baseAccounts.getList().then(function(accounts) {
$scope.allAccounts = accounts;
});
// GET /accounts
// 默认返回空数组. 想要从服务器返回值
// 需要給数组填充值. 以便在模板里使用
$scope.accounts = Restangular.all('accounts').getList().$object;
var newAccount = {name: "Gonto's account"};
// POST /accounts
baseAccounts.post(newAccount);
// GET http://www.google.com/ 这样设置URL
Restangular.allUrl('googlers', 'http://www.google.com/').getList();
// GET http://www.google.com/1 这样设置URL
Restangular.oneUrl('googlers', 'http://www.google.com/1').get();
// You can do RequestLess "connections" if you need as well
// GET /accounts/123/buildings/456
Restangular.one('accounts', 123).one('buildings', 456).get()
// GET to /accounts/123/buildings
Restangular.one('accounts', 123).getList('buildings')
// 一下使用诺言then
// GET /accounts
baseAccounts.getList().then(function (accounts) {
// Here we can continue fetching the tree :).
var firstAccount = accounts[0];
// 如下访问账户123 /accounts/123/buildings
$scope.buildings = firstAccount.getList("buildings");
// GET /accounts/123/places?query=param 携带请求头: x-user:mgonto
$scope.loggedInPlaces = firstAccount.getList("places", {query: param}, {'x-user': 'mgonto'})
// 这是regular JS 对象, 任意修改 :)
firstAccount.name = "Gonto"
// 如果想保留原值,可以克隆她
var editFirstAccount = Restangular.copy(firstAccount);
editFirstAccount.name = "New Name";
// PUT /accounts/123. 账户名将会修改
firstAccount.put();
editFirstAccount.put();
// PUT /accounts/123. 相应的保存将执行 POST 或 PUT
firstAccount.save();
// DELETE /accounts/123 删除第一个账户 :(
firstAccount.remove();
var myBuilding = {
name: "Gonto's Building",
place: "Argentina"
};
// POST /accounts/123/buildings 携带MyBuilding信息
firstAccount.post("Buildings", myBuilding).then(function() {
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
// GET /accounts/123/users?query=params
firstAccount.getList("users", {query: params}).then(function(users) {
// Instead of posting nested element, a collection can post to itself
// POST /accounts/123/users
users.post({userName: 'unknown'});
// 可以使用自定义方法 :).
// GET /accounts/123/users/messages?param=myParam
users.customGET("messages", {param: "myParam"})
var firstUser = users[0];
// GET /accounts/123/users/456. Just in case we want to update one user :)
$scope.userFromServer = firstUser.get();
// ALL http methods are available :)
// HEAD /accounts/123/users/456
firstUser.head()
});
}, function errorCallback() {
alert("Oops error from server :(");
})
// Second way of creating Restangular object. URL and ID :)
var account = Restangular.one("accounts", 123);
// GET /accounts/123?single=true
$scope.account = account.get({single: true});
// POST /accounts/123/messages?param=myParam with the body of name: "My Message"
account.customPOST({name: "My Message"}, "messages", {param: "myParam"}, {})