这是将Reveal与Ember.js一起使用的正确方法吗?
我试图在一个Ember应用程序中很好地实现,但没有成功。现在,对于pre.4版本,我又尝试了一次,结果非常简单,但我仍然怀疑我是否遵循了正确的模式。如果是这样的话,我希望这个例子能对你的项目有所帮助 使用:这是将Reveal与Ember.js一起使用的正确方法吗?,ember.js,zurb-foundation,Ember.js,Zurb Foundation,我试图在一个Ember应用程序中很好地实现,但没有成功。现在,对于pre.4版本,我又尝试了一次,结果非常简单,但我仍然怀疑我是否遵循了正确的模式。如果是这样的话,我希望这个例子能对你的项目有所帮助 使用: ember-1.0.0-pre.4.js 这是一个有效的例子 我有一个Post对象列表。单击列表项将打开一个模式对话框,并显示有关帖子的详细信息 为了显示列表,我创建了一个余烬应用程序,并为我的帖子模型提供了一个资源: window.App = Em.Application.create
- ember-1.0.0-pre.4.js
window.App = Em.Application.create();
App.Post = DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string')
});
App.Post.FIXTURES = [{
id: 1,
title: 'Lorem',
description: 'Lorem ipsum dolor sit amet'
}, {
id: 2,
title: 'Ipsum',
description: 'Fusce ante nulla'
}];
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.FixtureAdapter.create()
});
App.Router.map(function () {
this.resource('posts', {
path: '/'
});
});
App.PostsRoute = Em.Route.extend({
model: function () {
return App.Post.find();
}
});
对于模式对话框,我在应用程序模板中包含了一个渲染“显示”
<script type="text/x-handlebars">
{{outlet}}
{{render "reveal"}}
</script>
App.RevealController = Em.ObjectController.extend();
由于myRevealController的内容是Post模型的一个实例,因此我可以将其属性放入模板中:
<script type="text/x-handlebars" data-template-name="reveal">
<div id="myModal" class="reveal-modal large">
<h2>{{title}}</h2>
<p>{{description}}</p>
<a class="close-reveal-modal">×</a>
</div>
</script>
由于我可以在应用程序路径中定义OpenModel事件,我可以将RevealController的内容设置为通过的Post,并最终显示对话框:
App.ApplicationRoute = Em.Route.extend({
events: {
openModal: function (content) {
this.controllerFor('reveal').set('content', content);
$('#myModal').reveal();
}
}
});
就我目前对余烬的了解而言,这似乎是正确的做法,但我很高兴听到是否有任何改进。我认为您的方法绝对没有任何问题。
needs
更多的是关于依赖于另一个控制器的控制器,而您似乎只是简单地设置Ember.ArrayController的内容。您需要稍微更改您的实现,以适应Ember pre 5中这一点。对于@已弃用的控制器,您可以通过以下方式实现:
- 指定
PostsController
(我假设其他人)需要RevealController
,使用需要李>
- 将
RevealController
的实例传递到openModal
事件({{action openModal this controllers.reveal}}
)
当然,还有其他方法可以实现大致相同的目标。然而,我看到的好处如下:
- 在您的活动中,这对于测试等非常重要李>
- 您的
App.ApplicationRoute
没有责任找到控制器李>
一个缺点是,您将在少数几个能够调用Reveal的控制器中传递RevealController
。但我并不特别认为这是一个缺点,因为如果您发现自己指定了需求:['reveal']
alot,那么您总是可以创建一个抽象控制器,由ArrayController
扩展,因为需求
是一个串联属性(请参见余烬的Ember.ControllerMixin
中的concatenatedProperties
):
你觉得这个怎么样
另外,我能看到的您真正使用的唯一非余烬代码是:$(“#myModal”).reveal();
。是否有一种方法可以将显示代码放入App.RevealView
的didInsertElement
?然后在那里您可以执行类似的操作:this.get('element').reveal()
(可能是子视图?).漂亮的代码!读起来像华兹华斯的一首诗。Thx,但根据,我需要使用需要I.o.控制器。到目前为止,这还没有起作用。对不起,我写得太多,所以我在下面正确回答了。需要的用法很清楚,但在我的控制器中。显示似乎未定义(参见l36-40)。看不出我做错了什么…让它与最新提交的ember.js(post pre.4)一起工作。更新后,仍然有pre.4,因此它仅用于说明目的:)我认为,将openModal
保持在路由中似乎是正确的位置。在我看来,覆盖RevealView以封装对reveal方法的调用。我想这就是你的意思?
App.ApplicationRoute = Em.Route.extend({
events: {
openModal: function (content) {
this.controllerFor('reveal').set('content', content);
$('#myModal').reveal();
}
}
});
App.AbstractController = Ember.ArrayController.extend({
needs: ['reveal']
});
App.PostsController = App.AbstractController.extend({
// Since needs is a concatenated property, PostsController will have
// access to 4 controllers: reveal, another, andAnother, andYetAnother.
needs: ['another', 'andAnother', 'andYetAnother']
});