使用 AngularJS $resource 的放置操作的好例子
Good example of a put operation using AngularJS $resource
我一直在努力寻找使用 AngularJS $resource 的放置操作的一致且好的示例。我想更新但似乎无法更新的示例位于此处:
核心是,我需要了解在我上面 post 中提到的表单提交或投票应用程序中执行看跌操作的最佳 practice/normal 方法。有没有人有一个很好的例子来演示看跌期权?
如果您要在数据存储中创建新实体,则需要使用 POST/save。如果您正在更新与数据存储中已存在的实体相关联的数据,您要使用 PUT/update。当您只想更新实体数据的子集时,通常会保留补丁。
看看RFC
Several applications extending the Hypertext Transfer Protocol (HTTP)
require a feature to do partial resource modification. The existing
HTTP PUT method only allows a complete replacement of a document. This
proposal adds a new HTTP method, PATCH, to modify an existing HTTP
resource.
您将为 PUT 和 PATCH 操作提供一个 ID。您不会提供 POST 操作。
当我们加载我们的 angular 表单时,它通常是通过以下两种方式之一完成的。如果在我们创建新实体时加载了表单,那么我们将没有 id。我们将在控制器中知道这一点,并将调用 resource.save。如果我们为加载表单的控制器提供一个用于从端点提取数据以填充表单的 id,我们现在就拥有了可用于执行 resource.update 或 resource.patch 操作的 id,具体取决于如何我们正在更新的大部分实体。
这是一个处理更新和保存操作的示例保存函数。在这里,我们在进行资源调用之前检查是否通过路由提供了 id。
angular.module('appModule').controller('ExampleCtrl',
['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.saveForm = function () {
//Do input validation before you make a resource call
if ($routeParams.id) {
//call resource update since we have an id
}
else {
//call resource save since we don't have an id
}
};
}]);
这是 angularjs 文档中的示例:
如何创建自定义 PUT 请求:
var app = angular.module('app', ['ngResource', 'ngRoute']);
// Some APIs expect a PUT request in the format URL/object/ID
// Here we are creating an 'update' method
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', null,
{
'update': { method:'PUT' }
});
}]);
// In our controller we get the ID from the URL using ngRoute and $routeParams
// We pass in $routeParams and our Notes factory along with $scope
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
function($scope, $routeParams, Notes) {
// First get a note object from the factory
var note = Notes.get({ id:$routeParams.id });
$id = note.id;
// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id }, note);
// This will PUT /notes/ID with the note object in the request payload
}]);
我一直在努力寻找使用 AngularJS $resource 的放置操作的一致且好的示例。我想更新但似乎无法更新的示例位于此处:
核心是,我需要了解在我上面 post 中提到的表单提交或投票应用程序中执行看跌操作的最佳 practice/normal 方法。有没有人有一个很好的例子来演示看跌期权?
如果您要在数据存储中创建新实体,则需要使用 POST/save。如果您正在更新与数据存储中已存在的实体相关联的数据,您要使用 PUT/update。当您只想更新实体数据的子集时,通常会保留补丁。
看看RFC
Several applications extending the Hypertext Transfer Protocol (HTTP) require a feature to do partial resource modification. The existing HTTP PUT method only allows a complete replacement of a document. This proposal adds a new HTTP method, PATCH, to modify an existing HTTP resource.
您将为 PUT 和 PATCH 操作提供一个 ID。您不会提供 POST 操作。
当我们加载我们的 angular 表单时,它通常是通过以下两种方式之一完成的。如果在我们创建新实体时加载了表单,那么我们将没有 id。我们将在控制器中知道这一点,并将调用 resource.save。如果我们为加载表单的控制器提供一个用于从端点提取数据以填充表单的 id,我们现在就拥有了可用于执行 resource.update 或 resource.patch 操作的 id,具体取决于如何我们正在更新的大部分实体。
这是一个处理更新和保存操作的示例保存函数。在这里,我们在进行资源调用之前检查是否通过路由提供了 id。
angular.module('appModule').controller('ExampleCtrl',
['$scope', '$routeParams',
function($scope, $routeParams) {
$scope.saveForm = function () {
//Do input validation before you make a resource call
if ($routeParams.id) {
//call resource update since we have an id
}
else {
//call resource save since we don't have an id
}
};
}]);
这是 angularjs 文档中的示例:
如何创建自定义 PUT 请求:
var app = angular.module('app', ['ngResource', 'ngRoute']);
// Some APIs expect a PUT request in the format URL/object/ID
// Here we are creating an 'update' method
app.factory('Notes', ['$resource', function($resource) {
return $resource('/notes/:id', null,
{
'update': { method:'PUT' }
});
}]);
// In our controller we get the ID from the URL using ngRoute and $routeParams
// We pass in $routeParams and our Notes factory along with $scope
app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes',
function($scope, $routeParams, Notes) {
// First get a note object from the factory
var note = Notes.get({ id:$routeParams.id });
$id = note.id;
// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id }, note);
// This will PUT /notes/ID with the note object in the request payload
}]);