我试图了解这是否是在这里做事的最佳方式,因为对我来说它看起来像代码味道。我想要做的是,当我单击它时,我的按钮会变为加载状态,当控制器结束处理事件时它会变回默认状态(基本上是进行 AJAX 调用)
我的模板如下所示:
{{#view App.LoadingButton action="search" data-loading-text="content.loadingText"}}Search{{/view}}
我的观点是这样的:
App.LoadingButton = Ember.View.extend({
tagName: 'button',
classNames: ['btn', 'btn-primary'],
attributeBindings: ['type'],
type: 'button',
elem: null,
stateChanged: function () {
if (this.elem) {
var loading = this.get('controller').get('content.isLoading');
if (loading) {
$(this.elem).button('loading');
}
else {
$(this.elem).button('reset');
}
}
},
click: function (evt) {
this.elem = this.elem || evt.srcElement;
this.get('controller').send(this.get('action'));
},
didInsertElement: function () {
this.get('controller').addObserver('content.isLoading', this, 'stateChanged');
}
});
- 首先,我访问我的元素的方式看起来是错误的,当它被添加到 DOM 时,我不能获得对我的视图元素的引用吗?
- 其次,我依靠控制器从其模型中更改 isLoading 布尔标志,以便视图知道何时更改状态,这是以 Ember 方式实现它的最佳方式吗?
我看过很多关于 Ember 的视频,刚开始用它来实现,但要知道什么是最好的方法仍然不是一件容易的事,即使它可能像这样工作。