简体   繁体   English

我可以在ember中编写帮助程序以避免代码重复的地方

[英]Where I can write helper in ember to avoid code duplication

I have a couple lines of duplicate code 我有几行重复的代码

Ember.$(".close").trigger('click'); window.parent.showRegister();

in: adapters , controllers and routes . 在: adapterscontrollersroutes

Where the best place to write helper (function or action) and executing this in different places. 编写帮助程序(功能或操作)并在不同位置执行该程序的最佳位置。

For example I can write function in controller , but I don't know how access controllers in RESTAdapter . 例如我可以在controller编写函数,但是我不知道RESTAdapter如何访问controllers

The main reason why you can or can't access to some abstractions from another is control of software complexity, an attempt to keep it to a minimum. 可以或不能访问来自另一个的某些抽象的主要原因是控制软件的复杂性,试图将其降至最低。 There are principles like SOLID , low coupling / high cohesion to follow which helps us to maintain complex software systems, to increase comprehensibility of a system and to decrease possibility of an error in development process. 遵循诸如SOLID低耦合 / 高内聚性之类的原理,可以帮助我们维护复杂的软件系统,提高系统的可理解性并减少开发过程中出现错误的可能性。

Ember is follow the MVC pattern in some way, and because of that I wouldn't advise (1) to keep code for DOM manipulation in controller/adapter/router and (2) to couple adapters, controllers and routes in such a way. Ember以某种方式遵循MVC模式,因此,我不建议(1)将用于DOM操作的代码保留在控制器/适配器/路由器中,以及(2)以这种方式耦合适配器,控制器和路由。 I'am sure there is a way to put this code in View or in module outside of Ember.App classes; 我确定有一种方法可以将此代码放入View或Ember.App类之外的模块中; or to set up routing map handling this case. 或设置处理这种情况的路由图。

If it's not the case or you need a simple and straight solution, you can use ugly-magic constructions to access anything from everywhere in Ember: 如果不是这种情况,或者您需要一个简单直接的解决方案,则可以使用难看的魔术构造从Ember的任何地方访问任何内容:

App.__container__.lookup('controller:controllerName'); // controllers
App.__container__.lookup('router:main'); // routes
App.__container__.lookup('store:main'); // store, adapters, serializers
Ember.View.views['emberViewId'] // objects are dying here occasionally

Double underscore tells us that this is not the recommended way to build communications in application. 双下划线告诉我们,这不是在应用程序中建立通信的推荐方法。 :) :)

You can create a new Ember Object with helper as a method in it. 您可以使用辅助方法创建新的Ember对象。

Then you can register these helper in Application and inject it in controller , model and views. 然后,您可以在Application中注册这些帮助程序,并将其注入controller,model和view中。

see http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/#toc_dependency-injection-with-code-register-inject-code for more details 有关更多详细信息,请参见http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/#toc_dependency-injection-with-code-register-inject-code

App = Ember.Application.extend();
Ember.Application.initializer({
   name: 'logger',
   initialize: function(container, application){
   application.register('logger:main', {log: function(m){ console.log(m); }}, {instantiate: false});
   application.inject('route', 'logger', 'logger:main');
   }
});
App.create();

And use it like 并像这样使用

App.IndexRoute = Ember.Route.extend({
   activate: function(){
   // The logger property is injected into all routes
   this.logger.log('Entered the index route!');
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM