0

在 Ember 中,我们在模板中使用了很多视图助手。我想知道,当助手正在执行时(正在创建视图),是否有一种简单的方法可以将 mixin 传递给 Ember.View 类。

类似http://jsbin.com/ekuxal/2/edit

模板示例:

{{view Ember.TextField mixins="App.Console"}}

Mixin 示例:

App.Console = Ember.Mixin.create({
   didInsertElement: function(){
     this._super();
     console.log('from the mixin');
   }
});

从 mixin将登录到控制台。

有这样的东西有意义吗?

谢谢

4

2 回答 2

0

从我的角度来看,这个要求对我来说毫无意义。为什么?Mixin 在扩展类时使用,因为它们是实现多重继承的一种方式。因此它们在数据和逻辑建模期间使用。视图助手用于实例化视图类的对象。我认为,可以通过编写自定义帮助程序并在其中使用createWithMixins()来实现。但是在模板中做这样的事情似乎很混乱。

你能再澄清一下你的要求吗?真的只是关于记录吗?

更新:实施提案

我通读了视图助手的源代码,以评估这是否可能。这不是 Ember 的开箱即用功能,但可以通过覆盖 Ember View 帮助程序中的一些逻辑来实现。

假设你有这个模板:

{{view App.YourView mixin="App.YourMixin"}}

Helper 最重要的是实例 Ember.ViewHelper,其中包含要修改的逻辑。可以尝试重新打开它并像这样修改相关部分:

var EmberHandlebars = Ember.Handlebars;
EmberHandlebars.ViewHelper.reopen({
  helper: function(thisContext, path, options) {
    var inverse = options.inverse,
        data = options.data,
        view = data.view,
        fn = options.fn,
        hash = options.hash,
        newView;

    if ('string' === typeof path) {
      newView = EmberHandlebars.get(thisContext, path, options);
      Ember.assert("Unable to find view at path '" + path + "'", !!newView);
    } else {
      newView = path;
    }

    // NEW CODE FOR MIXIN LOGIC
    if(hash.mixin){
      var newViewClass = newView; // newView should be the class App.YourView here
      var yourMixin = Ember.get(hash.mixin); //get the Mixin by name
      var newViewInstance = newView.createWithMixins(yourMixin); //create the view with mixin
      newView = newViewInstance; // assign the view to the variable where the rest of the logic is expecting it
    }

    Ember.assert(Ember.String.fmt('You must pass a view to the #view helper, not %@ (%@)', [path, newView]), Ember.View.detect(newView) || Ember.View.detectInstance(newView));

    var viewOptions = this.propertiesFromHTMLOptions(options, thisContext);
    var currentView = data.view;
    viewOptions.templateData = options.data;
    var newViewProto = newView.proto ? newView.proto() : newView;

    if (fn) {
      Ember.assert("You cannot provide a template block if you also specified a templateName", !get(viewOptions, 'templateName') && !get(newViewProto, 'templateName'));
      viewOptions.template = fn;
    }

    // We only want to override the `_context` computed property if there is
    // no specified controller. See View#_context for more information.
    if (!newViewProto.controller && !newViewProto.controllerBinding && !viewOptions.controller && !viewOptions.controllerBinding) {
      viewOptions._context = thisContext;
    }

    currentView.appendChild(newView, viewOptions);
  } 
});

注意:我自己还不能测试这个逻辑。 更新:测试成功!

于 2013-03-15T08:01:19.940 回答
0

在这种情况下,我将扩展 Ember 的 TextField 视图以创建您自己的自定义视图,并将 mixin 添加到您在 javascript 中扩展视图的位置。例如:

模板:

{{view 'MyTextField'}} // Custom view that extends Ember.textField

看法:

App.MyTextFieldView = Em.TextField.extend(App.MyMixin, { // Add your mixin to Ember's TextField view here
    // Any further view code here (optional because this view already has all the functions of Ember's text field view and you've mixed in your other functionality in the line above).
});

混心:

App.MyMixin = Em.Mixin.create({
    // Your mixin code here
});
于 2013-12-24T02:11:45.830 回答