0

我希望能够在 _Templated 对象发生某些事情时发布特定主题。现在,我只是简单地创建一些小部件混合:

[...]
return declare('hotplate.hotDojoAuth.LoginForm', [_WidgetBase, _TemplatedHooksMixin, _TemplatedMixin, _WidgetsInTemplateMixin ], {

_TemplatedHooksMixin 简单地发出:

define([
  'dojo/_base/declare',
  'dojo/_base/lang',
  'dojo/topic',

  ], function(
    declare
  , lang
  , topic

  ){
    return  declare(null, {

      templatedHooks: true,

      constructor: function(){
        this.templatedHooks = true;
        topic.publish('hotplate/hotHooks/constructor', this);
      },

      buildRendering: function(){
        topic.publish('hotplate/hotHooks/buildRendering/before', this);
        this.inherited(arguments);
        topic.publish('hotplate/hotHooks/buildRendering/after', this);
      },

      destroyRendering: function(){
        topic.publish('hotplate/hotHooks/destroyRendering/before', this);
        this.inherited(arguments);
        topic.publish('hotplate/hotHooks/destroyRendering/after', this);
      },

      postCreate: function(){
        topic.publish('hotplate/hotHooks/postCreate/before', this);
        this.inherited(arguments);
        topic.publish('hotplate/hotHooks/postCreate/after', this);
      },

      startup: function(){
        topic.publish('hotplate/hotHooks/startup/before', this);
        this.inherited(arguments);
        topic.publish('hotplate/hotHooks/startup/after', this);
      },

      destroy: function(){
        topic.publish('hotplate/hotHooks/destroy/before', this);
        this.inherited(arguments);
        topic.publish('hotplate/hotHooks/destroy/after', this);
      }

    });
  }
);

问题:

1)代码重复,主要是因为它使用了“this”、“arguments”、“inherited”,都在尖叫“Don't duck with me!” (尤其是this.inherited)。. 关于使用简单参数制作一个函数的任何提示?

2)这是一种半理智的做法吗?这个想法是允许与我的库无关的其他小部件更改 _Templated 小部件的内容。

3)如果这是一条好的路径(评论?),你认为我所说的路径是理智的吗?

谢谢!

默克。

4

1 回答 1

1

我认为你应该看看使用dojo/aspect

http://dojotoolkit.org/reference-guide/1.8/dojo/aspect.html

constructor: function(){
    this.templatedHooks = true;

    var methodsToDecorate = ["buildRendering", "destroyRendering", ...];
    array.forEach(methodsToDecorate, function(methodName) {
        aspect.before(this, methodName, function(deferred){
            topic.publish('hotplate/hotHooks/' + methodName + '/before', this);
        });
        aspect.after(this, methodName, function(deferred){
            topic.publish('hotplate/hotHooks/' + methodName + '/after', this);
        });
    });

    topic.publish('hotplate/hotHooks/constructor', this);
},
于 2012-10-29T13:20:35.753 回答