假设我有以下形式,包括模型和嵌套模型:
<label>Company Name</label>
<input type="text" ng-model="company.name" />
<label>Owner Name</label>
<input type="text" ng-model="company.owner.name" />
Run Code Online (Sandbox Code Playgroud)
我发布的内容如下:
Restangular.all('companies').post($scope.company);
Run Code Online (Sandbox Code Playgroud)
我期望在服务器端(在本例中为Rails)是嵌套的哈希,如下所示:
company:
name: Test Company
owner:
name: Test Owner
Run Code Online (Sandbox Code Playgroud)
但我得到的是这个:
name: Test Company
company:
name: Test Company
owner:
name: Test Owner
Run Code Online (Sandbox Code Playgroud)
看起来模型正在变平,并且第一模型的字段也在范围之外重复.
如何在保持嵌套的同时发布模型,并且最好不要在散列中重复其范围之外的模型字段?
假设我有一个包含多个字段的资源,其中一些是只读的.或者它们可能属于我想在服务器上以不同方式处理的不同用例.
例如,我的bing资源如下所示:
{id: 1,
foo: "A",
bar: "B",
createdAt: "2013-05-05"}
Run Code Online (Sandbox Code Playgroud)
我想让Restangular到PUT只有一些字段,执行如下请求:
PUT /bing/1 {foo: "A"}
PUT /bing/1 {bar: "B"}
PUT /bing/1 {foo: "A", bar: "B"}
Run Code Online (Sandbox Code Playgroud)
我确实不希望做的是:
PUT /bing/1 {id: 1, foo: "A", bar: "B", createdAt: "2013-05-05"}
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它?
我想使用Restangular customGET方法在查询参数中使用特殊字符调用URL .我正在为我的API使用Loopback,它使用方括号进行查询.似乎在Restangular中不允许这样做.
我想调用以下URL.
/api/v1/employees/findOne?filter[where][salesCode]=SC2
Run Code Online (Sandbox Code Playgroud)
或者这个但不确定如何.
/api/v1/employees?filter[where][salesCode]=SC2
Run Code Online (Sandbox Code Playgroud)
我试过跟随没有成功.
Restangular.all("employees").customGET("findOne", {filter + "%5Bwhere%5D%5BsalesCode%5D": newValue});
Run Code Online (Sandbox Code Playgroud)
和
Restangular.all("employees").customGET("findOne", {filter[where][salesCode]: newValue});
Run Code Online (Sandbox Code Playgroud)
作为一个我正在使用的工作,$http但黑客是一天的黑客.
这能张贴到服务器departments是null
JavaScrtipt代码:
var departments = [{ DepartmentName : "Name", DepartmentId : 1}];
Restangular.all('records/StartNewDate').post(departments);
Run Code Online (Sandbox Code Playgroud)
Web Api控制器
public HttpResponseMessage StartNewDate(DepartmentViewModel[] departments)
{
....
....
}
Run Code Online (Sandbox Code Playgroud)
服务器模型
public class DepartmentViewModel
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我有这样的服务:
.factory('Car', function(Restangular) {
var baseCars = Restangular.all('cars');
Restangular.extendModel('cars', function(obj) {
obj.extraMethod = function() { console.log('method added'); };
return obj;
});
return {
show: function(id) {
return baseCars.one(id).get();
}
};
});
Run Code Online (Sandbox Code Playgroud)
和这样的控制器:
.controller('CarCtrl', function ($scope, $stateParams, Car) {
Car.show($stateParams.id).then(function(car) {
$scope.car = car;
$scope.car.extraMethod();
});
});
Run Code Online (Sandbox Code Playgroud)
但是,这会导致undefined is not a function错误$scope.car.newMethod().
有谁知道这是为什么以及如何让它工作?
我正在使用Restangular同时对服务器进行几次调用.Restangular返回每个动作的承诺,每个动作都需要对其特定的返回做一些事情,一旦所有3个完成并且它们的.then功能已经完成,我需要执行另一个功能.
这是在这种情况下做承诺的正确方法吗?
var a, b, c;
a = service1.getList(...).then(function A(data) { do something; $log.debug('a'); return 'a';});
b = service2.getList(...).then(function B(data) { do something; $log.debug('b'); return 'b';});
c = service3.getList(...).then(function C(data) { do something; $log.debug('c'); return 'c';});
$q.all([a,b,c]).then(function wrapUp(values) { $log.debug(values); do something after functions A, B,and C above have finished;}
Run Code Online (Sandbox Code Playgroud)
我不知道$ q.all()是否会在A,B和C的最后运行,或者它是否可以在最后一个函数前面竞争.wrapUp()总是最后被调用吗?
注意A,B和C是承诺a,b和c完成后调用的函数.
传递给wrapUp的values数组似乎是来自A,B和C的返回值.但是,在我的控制台中,函数A记录10次,然后函数B记录10次,然后函数C和函数wrapUp都记录10次(交替).
10x伐木似乎有些东西被打破.....,恐惧的种族情况似乎是可能的....
有人能解释一下这里发生了什么吗?

我正在点击一个API,它要求所有经过身份验证的操作都在请求中包含一个身份验证令牌,但是,在我登录之前,我没有身份验证令牌.
我只看到了在Restangular中设置默认请求参数的示例app.config.是否可以在用户登录并User.auth_token设置之后设置此项?
所以基本上代替:
app.config(function(RestangularProvider) {
RestangularProvider.setDefaultRequestParams({
auth_token: 'thisistheauthenticationtoken'
});
});
Run Code Online (Sandbox Code Playgroud)
我需要:
app.config(function(RestangularProvider) {
RestangularProvider.setDefaultRequestParams({
auth_token: User.auth_token
});
});
Run Code Online (Sandbox Code Playgroud) 我创建了一个UserService,如下所示:
angular.module('nrApp').factory('userService', ['Restangular', 'UserModel', 'DSCacheFactory', function (Restangular, UserModel, DSCacheFactory) {
// Create a new cache called "profileCache"
var userCache = DSCacheFactory('userCache', {
maxAge: 3600000,
deleteOnExpire: 'aggressive',
storageMode: 'localStorage', // This cache will sync itself with `localStorage`.
onExpire: function (key, value) {
Restangular.oneUrl('users', key).get().then(function(data) {
userCache.put(key, data);
});
}
});
Restangular.extendModel('users', function(obj) {
return UserModel.mixInto(obj);
});
Restangular.addRequestInterceptor(function(element, operation, what, url) {
if(operation === 'get') {
debugger;
//Check the cache to see if the resource is already cached
var data = userCache.get(url); …Run Code Online (Sandbox Code Playgroud) 官方的restangular文档提供此代码示例以在ErrorInterceptor中重试请求:
var refreshAccesstoken = function() {
var deferred = $q.defer();
// Refresh access-token logic
return deferred.promise;
};
Restangular.setErrorInterceptor(function(response, deferred, responseHandler) {
if(response.status === 403) {
refreshAccesstoken().then(function() {
// Repeat the request and then call the handlers the usual way.
$http(response.config).then(responseHandler, deferred.reject);
// Be aware that no request interceptors are called this way.
});
return false; // error handled
}
return true; // error not handled
});
Run Code Online (Sandbox Code Playgroud)
但是,正如它在评论中所说,第二次尝试不会触发任何拦截器,因为它直接使用$ http.
有没有办法让第二个请求也通过Restangular管道并执行ErrorInterceptor?
我正在使用restangular,但我有"Put"方法的问题,它没有按预期工作
我的angularService代码
var userService = function (restangular) {
var resourceBase = restangular.all("account/");
restangular.addResponseInterceptor(function (data, operation, what, url, response, deferred) {
if (operation == "getList") {
return response.data;
}
return response;
});
this.getUserById = function (id) {
return resourceBase.get(id);
// return restangular.one("account", id).get();
};
this.updateUser = function(user) {
return user.Put();
};
}
Run Code Online (Sandbox Code Playgroud)
我的控制器代码
var userEditController = function (scope, userService, feedBackFactory, $routeParams) {
scope.user = undefined;
scope.updateUser = function () {
userService.updateUser(scope.user).then(function (data) {
feedBackFactory.showFeedBack(data);
}, function (err) {
feedBackFactory.showFeedBack(err);
});
}; …Run Code Online (Sandbox Code Playgroud) angularjs ×10
restangular ×10
javascript ×3
asp.net ×1
caching ×1
loopbackjs ×1
promise ×1
rest ×1