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

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

问题描述

我有一个角控制器在读取创建资源:

  angular.module('adminApp')
  .controller('PropertiesCtrl',函数($日志$范围,物业,$位置){
    $ scope.properties = Property.query()
  });

现在我想逻辑添加到控制器才能够创造一个属性资源:

  angular.module('adminApp')
  .controller('PropertiesCtrl',函数($日志$范围,物业,$位置){
    $ scope.properties = Property.query()
    $ scope.create =功能(){
      //逻辑创建
    };
  });

然而,当我在窗体上创建一个属性,不必要的调用,以第一次提取的所有属性。我该如何避免这种情况?


潜在的解决方案?


  1. 我可以为创建,不会取物业所专门创建一个单独的控制器。然而,这将使其更简单封装单个控制器根据用于单个资源的所有CRUD操作。

  2. 我可以创建一个函数来获取所有属性。然而,我的索引页使用属性直接。我首先需要获取数据调用一些方法,然后使用数据(不知何故?)


解决方案

我的反应是,它听起来像你试图用一个控制器作为一种服务,就像你正试图把许多功能集成到一个控制器。

因此​​,有你应该考虑两个主要的事情。首先,它创建控制器只有一个特定的目的的每一个相当重要。注意,这是的的作为使用控制器只有一次同样的事情,你encuraged如果有应该出现在多个地方一个功能,使用相同的控制器在几个不同的地方。它只是意味着控制器不能同时做几件事情。

让我们来照相馆作为一个例子。虽然你可以创建一个控制器获取所有的相关的照片,让你添加新的照片,并允许您编辑和删除现有的照片于一身,这将是一个坏主意。如果你决定将照片添加也可以从另一个页面,页面X做什么?如果你在哪里重复使用相同的控制器,那么你也将是从服务器请求画廊和设置的事情控制你不打算要在该网页。

如果你不是做了一个控制器,仅用于获取内容,添加新的照片,另一个用于编辑等单独的控制器负责,那么这将是容易的。你只落实第X页的创建控制器,你不必对不期而遇担心引发更多的比你想要的。您可以选择实现正是你想要的网页上的只有的该功能的功能。这也让你的控制器体积小,易于阅读和快速编辑/ bug修正,所以这是一个双赢/胜利!

其次,如果你要收集所有的CRUD资源到一个对象(这点我也愿意这样做),他们不应该在一个控制器,他们应该在的服务的。所以,你有一个PhotoAPI暴露出创建,读取,更新和删除功能。那么你的指数控制器只是调用读功能,您创建控制器创建函数等控制器确定哪些功能和数据的地方,但逻辑是在组合服务。这样,你可以聚集你的资源,使他们很容易找到,不具有组合控制器产生的问题。

所以是这样的:

  app.service('PhotoAPIService',[
功能(){
   this.READ =功能(){
     //读取逻辑
   }  this.CREATE =功能(){
     //创建逻辑
   }
}]);app.controller('PhotoIndexController',[
'$范围',
PhotoAPIService',
功能($范围,PhotoAPIService){
   $ scope.photos = PhotoAPIService.READ(<数据&GT);
}]);
app.controller('PhotoCreateController',[
'$范围',
PhotoAPIService',
功能($范围,PhotoAPIService){
   $ scope.createPhoto = PhotoAPIService.CREATE;
}]);

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?


Potential solutions?

  1. I could create a separate controller specifically for the creation of the Property that would not fetch the properties. However, it would make it simpler to encapsulate all CRUD operations for a single resource under a single controller.
  2. I could create a function to fetch all properties. However, my index page uses "properties" directly. I would first need to fetch the data calling some method and then using the data (somehow?)

解决方案

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.

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.

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!

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.

So something like:

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天全站免登陆