从我的角度来看,这个要求对我来说毫无意义。为什么?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);
}
});
注意:我自己还不能测试这个逻辑。
更新:测试成功!