我需要这样做来覆盖Ember.LinkView
的调用transitionTo
,以便为过渡之间的 jQuery 动画提供解决方案。在我看来,有几种可行的方法可以覆盖 LinkView。我成功的第二个是 Trek 的最后一个选项,而且更简单。这是方法#2:
方法#2
{{#linkTo 'items' this eventName="myEvent"}} Link to {{title}} {{/linkTo}}
现在重写应用范围的 LinkView:
Ember.LinkView.reopen({
// this handler is still called on click, but
// if we specify eventName in our template,
// we can call that handler only when we need to,
// or not at all
click: function (e) {
var evtName = this.get('eventName');
// transitionTo was already invoked by
// this._invoke() if evtName was `click`
if (evtName === 'click') return;
e.preventDefault();
// do some stuff here
var args = [].slice.call(arguments);
this.trigger.apply(this, [evtName].concat(args));
}
});
方法#1
我想出的第一个方法是扩展Ember.LinkView
和创建自定义 Handlebars 助手。Ember 源代码在这里阅读起来非常方便,但我不得不重写一个私有方法,所以我认为这不是很理想。这是实现。请记住,我试图控制 View 何时触发transitionTo
:
{{#appLinkTo 'items' this}} Link to {{title}} {{/appLinkTo}}
现在编码吧!
App.LinkView = Ember.LinkView.extend({
// always called after this.invoke(),
// which calls transitionTo
click: function (e) {
e.preventDefault();
},
// already bound to the click event by this.init().
// our click handler above always gets called after this one
_invoke: function (event) {
// we need to simulate the old _invoke if we
// want to override its call to transitionTo
//
// https://github.com/emberjs/ember.js/blob/v1.0.0/packages/ember-routing/lib/helpers/link_to.js#L297
var isSimpleClick = Ember.ViewUtils.isSimpleClick;
if (!isSimpleClick(event)) { return true; }
event.preventDefault();
if (this.bubbles === false) { event.stopPropagation(); }
if (this.get('_isDisabled')) { return false; }
if (this.get('loading')) {
Ember.Logger.warn("This link-to is in an inactive loading state because at least one of its parameters presently has a null/undefined value, or the provided route name is invalid.");
return false;
}
// now we can start messing around
var routeArgs = this.get('routeArgs');
// routeArgs seems to have format ['routeName', models for dynamic segments]
this.set('routeArgs', ['group', routeArgs[1]]);
// if we use:
this.get('controller').send('someAction', routeArgs);
// the controller can do in its `someAction` handler:
// `this.transitionToRoute.apply(this, routeArgs);`
}
});
// besides the naming, this is verbatim from the end of:
// https://github.com/emberjs/ember.js/blob/v1.0.0/packages/ember-routing/lib/helpers/link_to.js
Ember.Handlebars.registerHelper('app-link-to', function(name) {
var options = [].slice.call(arguments, -1)[0],
params = [].slice.call(arguments, 0, -1),
hash = options.hash;
hash.disabledBinding = hash.disabledWhen;
hash.parameters = {
context: this,
options: options,
params: params
};
return Ember.Handlebars.helpers.view.call(this, App.LinkView, options);
});
Ember.Handlebars.registerHelper('appLinkTo', Ember.Handlebars.helpers['app-link-to']);
方法#3
如果你想要两者兼得,你可以结合这两个方法和 extend Ember.LinkView
,创建一个自定义的 Handlebars 助手,并使用自定义事件名称来表示你想要采取的行动。这样,就不需要覆盖Ember.LinkView
和覆盖。_invoke
祝你好运!