对所有 CRUD 操作使用相同的控制器(类似 Rails) [英] Using same controller for all CRUD operations (Rails-alike)

查看:23
本文介绍了对所有 CRUD 操作使用相同的控制器(类似 Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在创建时获取资源的角度控制器:

I have an angular controller that fetches a resource on creation:

angular.module('adminApp')
  .controller('PropertiesCtrl', function ($log, $scope, Property, $location) {
    $scope.properties = Property.query()  
  });

现在我想向控制器添加逻辑以能够创建属性"资源:

Now I want to add logic to the controller to be able to create a "Property" resource:

angular.module('adminApp')
  .controller('PropertiesCtrl', function ($log, $scope, Property, $location) {
    $scope.properties = Property.query()  
    $scope.create = function(){
      //logic to create
    };
  });

但是,当我在表单上创建属性"时,会进行不必要的调用以首先获取所有属性.我如何避免这种情况?

However, when I am on the form to create a "Property", an unnecessary call is made to fetch all the properties first. How do I avoid this?

可能的解决方案?

  1. 我可以创建一个单独的控制器,专门用于创建不会获取属性的属性.但是,将单个资源的所有 CRUD 操作封装在单个控制器下会更简单.
  2. 我可以创建一个函数来获取所有属性.但是,我的索引页直接使用属性".我首先需要调用一些方法来获取数据,然后使用数据(以某种方式?)

推荐答案

我的反应是,您似乎在尝试将控制器用作服务,并且尝试将许多功能放入一个控制器中.

My reaction is that it sounds like you are trying to use a controller as a service, and like you are trying to put to many features into one controller.

因此,您应该考虑两件主要事情.首先,创建控制器非常重要,每个控制器只有一个特定的目的.请注意,这与仅使用一次控制器不同,如果您的功能应该出现在多个地方,则鼓励您在多个不同的地方使用相同的控制器.这只是意味着控制器不应该同时做几件事.

So there are two main things you should think about. First of all it is fairly important create controller that only have one specific purpose each. Note that this is not the same thing as using the controller only once, you are encuraged to use the same controller in several different places if you have a feature that should appear in more than one place. It just means that the controller shouldn't be doing several things at once.

让我们以照片库为例.虽然您可以创建一个控制器来获取所有照片、添加新照片以及编辑和删除现有照片,但这不是一个好主意.如果您决定也可以从另一个页面页面 X"添加照片怎么办?如果您在何处重复使用相同的控制器,那么您还需要从服务器请求图库并为您不打算出现在该页面上的内容设置控件.

Let's take a photo gallery as an example. While you could create one controller that gets all the photos, lets you add new photos, and lets you edit and delete existing photos all in one, this would be a bad idea. What if you decide that adding a photo can also be done from another page, "Page X"? If you where to reuse the same controller then you would also be requesting the gallery from the server and setting up controls for things you don't intend to be on that page.

如果您改为制作一个仅负责获取内容的控制器、一个用于添加新照片的单独控制器、另一个用于编辑等的控制器,那么这将很容易.您只需在Page X"上实现 create-controller,您就不必担心意外触发超过您想要的次数.您可以选择在页面上完全实现您想要的功能,并且实现该功能.这也使您的控制器体积小、易于阅读并且可以快速编辑/修复错误,因此它是双赢的!

If you instead made one controller that is only responsible for getting the content, a separate controller for adding new photos, another one for editing, etc, then it would be easy. You would just implement the create-controller on "Page X" and you don't have to worry about accidentaly triggering more than you wanted. You can choose to implement exactly the feature you want on a page and only that feature. This also keeps your controllers small, easy to read and quick to edit/bugfix, so it's a win/win!

其次,如果您想将所有 CRUD 资源收集到一个对象中(我也想这样做),它们不应位于控制器中,而应位于服务中.所以你有一个 PhotoAPI 来公开 CREATE、READ、UPDATE 和 DELETE 函数.然后你的索引控制器只调用 READ 函数,你的创建控制器调用 CREATE 函数等等.控制器定义了哪些函数和数据在何处可用,但逻辑​​在组合服务中.这样您就可以将您的资源集中起来,以便于找到它们,而不会产生组合控制器的问题.

Secondly, if you want to gather all your CRUD resources into one object (which I would also want to do), they should not be in a controller, they should be in a service. So you have one PhotoAPI which exposes CREATE, READ, UPDATE, and DELETE functions. Then your index controller just calls the READ function, your create controller the CREATE function etc. The controllers define what functions and data are available where, but the logic is in the combined service. That way you can clump your resources to make them easy to find, without creating the problems with having a combined controller.

比如:

app.service('PhotoAPIService', [
function() {
   this.READ = function() {
     // Read logic
   }

  this.CREATE = function() {
     // Create logic
   }
}]);

app.controller('PhotoIndexController', [
'$scope',
'PhotoAPIService',
function($scope, PhotoAPIService) {
   $scope.photos = PhotoAPIService.READ(<data>);
}]);


app.controller('PhotoCreateController', [
'$scope',
'PhotoAPIService',
function($scope, PhotoAPIService) {
   $scope.createPhoto = PhotoAPIService.CREATE;
}]);

这篇关于对所有 CRUD 操作使用相同的控制器(类似 Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆