dry

AngularJS DRY controller structure

泪湿孤枕 提交于 2019-12-04 16:22:34
The code below represents a situation where the same code pattern repeats in every controller which handles data from the server. After a long research and irc talk at #angularjs I still cannot figure how to abstract that code, inline comments explain the situations: myApp.controller("TodoCtrl", function($scope, Restangular, CalendarService, $filter){ var all_todos = []; $scope.todos = []; Restangular.all("vtodo/").getList().then(function(data){ all_todos = data; $scope.todos = $filter("calendaractive")(all_todos); }); //I can see myself repeating this line in every //controller dealing with

Django - Tips to avoid repeating code in views

不打扰是莪最后的温柔 提交于 2019-12-04 13:05:35
问题 I'm moving from a PHP background into Django development via python, mostly for the sake of tackling a MVC (or MVT) that I feel makes the most sense, although in this pattern I've started to notice a lot of repeated code in my views. For example, when logged in I have information regarding the user that I would like to appear on every page, although when using render_to_response and in every view this is required I have to grab the information and pass it to the render_to_response function. I

DRY vs Security and Maintainability with MVC and View Models

a 夏天 提交于 2019-12-04 12:14:04
问题 I like to strive for DRY, and obviously it's not always possible. However, I have to scratch my head over a concept that seems pretty common in MVC, that of the "View Model". The View Model is designed to only pass the minimum amount of information to the view, for both security, maintainability, and testing concerns. I get that. It makes sense. However, from a DRY perspective, a View Model is simply duplicating data you already have. The View Model may be temporary, and used only as a DTO,

How to write a Rails mixin that spans across model, controller, and view

强颜欢笑 提交于 2019-12-04 08:42:59
问题 In an effort to reduce code duplication in my little Rails app, I've been working on getting common code between my models into it's own separate module, so far so good. The model stuff is fairly easy, I just have to include the module at the beginning, e.g.: class Iso < Sale include Shared::TracksSerialNumberExtension include Shared::OrderLines extend Shared::Filtered include Sendable::Model validates_presence_of :customer validates_associated :lines owned_by :customer def initialize( params

DRYing up a generic RedisTemplate in Spring 4

我只是一个虾纸丫 提交于 2019-12-04 07:50:55
I've read that you can have @Autowired generics as of Spring 4, which is awesome. I have an abstract RedisService class in which I want to have @Autowired a generic RestTemplate, like so: public abstract class RedisService<T> implements InitializingBean { private final String VALUE_KEY_PREFIX; private final String SET_KEY; @Autowired private RedisTemplate<String, T> valueTemplate; @Autowired private StringRedisTemplate stringTemplate; private SetOperations<String, String> setOperations; private ValueOperations<String, T> valueOperations; // and so on... } My Java config for the valueTemplate s

setattr and getattr with methods

雨燕双飞 提交于 2019-12-04 07:29:31
I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): self.refClass.action1() def action2(self): self.refClass.action2() def action3(self): self.refClass.action3() This is the refClass: class RefClass(): def __init__(self): self.myClass = MyClass(self) def action1(self): #Stuff to execute action1 def action2(self): #Stuff to execute action2 def action3(self): #Stuff to execute action3 I'd like to use Python Metaprogramming to make this more elegant and

Rails 3 strip whitespace before_validation on all forms

别说谁变了你拦得住时间么 提交于 2019-12-04 06:29:19
I'm relatively new to Rails and a bit surprised this isn't a configurable behavior...at least not one I've been able to find yet?!? I would have thought that 99% of forms would benefit from whitespace being trimmed from all string & text fields?!? Guess I'm wrong... Regardless, I'm looking for a DRY way to strip all whitespace from form fields (of type :string & :text) in a Rails 3 app. The Views have Helpers that are automatically referenced (included?) and available to each view...but Models don't seem to have such a thing?!? Or do they? So currently I doing the following which first

Circular dependencies versus DRY

感情迁移 提交于 2019-12-04 06:25:51
I'm designing a re-usable class library that contains 2 assemblies (amongst others) named core.xml.dll and core.string.dll. The xml assembly references the string assembly in order to use some string helper methods. However now there is an string method that would be benefit from using a method contained in the xml assembly. If I reference the xml assembly from the string assembly I will have created a circular dependency and will be unable to build both assemblies from source code. (ie chicken and the egg problem). In order to follow the "Don't Repeat Yourself" principle I would like to avoid

Share Constants Between PHP and JavaScript [duplicate]

谁都会走 提交于 2019-12-04 01:09:47
This question already has answers here : Closed 7 years ago . Possible Duplicate: Pass a PHP string to a Javascript variable (and escape newlines) I have several constants in a PHP application I'm developing. I've defined a Constants class and the defined the constants as const VAR_NAME = value; in this class. I would like to share these constants between my JavaScript and PHP code. Is there a DRY (Don't Repeat Yourself) mechanism to share them? class Constants { const RESOURCE_TYPE_REGSITER = 2; const RESOURCE_TYPE_INFO = 1; } Adam Peck I would use json_encode . You will have to convert the

rails - DRY respond_to with repeated actions

落爺英雄遲暮 提交于 2019-12-04 00:15:00
问题 In one of my rails controller, I must respond to several types of formats, so I use the typical respond_to chain: respond_to do |format| format.html { ... } format.mobile { ... } format.jpg { ... } format.xml { ... } format.js { ... } end Usually that the { ... } part is repeated on several formats. What is the best way to stay DRY on this case? On an scenario in which html , mobile and xml have a "repeated" action, I'd like to do something like this: respond_to do |format| format[:html,