简体   繁体   English

Ember.JS响应式模板

[英]Ember.JS responsive template

This question is more about understand other people's point of view in other to have a more scalable Ember.JS application and clarify what would be the best approach to deal with this kind of situation. 这个问题更多的是了解其他人在其他人的观点,以获得更具可扩展性的Ember.JS应用程序,并阐明处理这种情况的最佳方法。

I have a template called posts.hbs and I present a totally different structure for mobile and desktop. 我有一个名为posts.hbs的模板,我提出了一个完全不同的移动和桌面结构。

However, all the functionalities are the same, which means that in my controllers, the same actions and functions called on desktop are called on mobile. 但是,所有功能都是相同的,这意味着在我的控制器中,在桌面上调用的相同操作和功能在移动设备上调用。

To verify if the user is accessing the site with a mobile device or desktop, I user the following condition on my template: 要验证用户是否使用移动设备或桌面访问该网站,我在我的模板上使用以下条件:

{{#if dekstop}}

  {{!-- desktop structure (about 250 lines!) --}}

{{else}} 

  {{!-- mobile structure (about 250 lines!) --}}

{{/if}}

The issue here is that, because the both mobile and desktop structures evolves a lot of components and HTML, I have a with more than 500 lines. 这里的问题是,因为移动和桌面结构都演变了很多组件和HTML,所以我拥有超过500行。 It's too big and it's becoming confusing to maintain this code. 它太大了,维护这段代码变得令人困惑。

One alternative that I see is create two different templates: 我看到的另一种选择是创建两个不同的模板:

  • posts-desktop.hbs 帖子-desktop.hbs
  • posts-mobile.hbs 帖子-mobile.hbs

As well as: 以及:

  • controllers/posts-desktop.js 控制器/帖-desktop.js
  • controllers/posts-mobile.js 控制器/帖-mobile.js

And: 和:

  • route/posts-desktop.js 路线/职位,desktop.js
  • route/posts-mobile.js 路线/职位,mobile.js

Then on my respective controllers and routes, I can call mixins containing all the common functionalities. 然后在我各自的控制器和路由上,我可以调用包含所有常用功能的mixin。 This way, I keep the functionalities in one place and I can have my templates in separated places. 这样,我将功能保存在一个地方,我可以将模板放在分开的位置。

Additional information: 附加信息:

  • I am using Ember.JS 1.11.0 我使用的是Ember.JS 1.11.0

If I understand your question correctly, mobile and desktop viewers see a website with the same functionality, and you are using conditional code only to show different layouts of the site to users based on their device. 如果我正确理解您的问题,移动和桌面查看器会看到具有相同功能的网站,并且您仅使用条件代码根据用户的设备向用户显示不同的网站布局。

If your concerns are creating code that is easy to maintain, and a site which is responsive and adapts to different viewports, then have you considered using standard responsive and adaptive design techniques? 如果您的关注点是创建易于维护的代码,以及响应并适应不同视口的站点,那么您是否考虑过使用标准的响应式和自适应设计技术?

Instead of your conditional code all users get the same HTML, you use the one template, and then rely on standard responsive design techniques and media queries to change the screen layout based on viewport size. 您使用一个模板,而不是条件代码,所有用户都获得相同的HTML,然后依靠标准的响应式设计技术和媒体查询来根据视口大小更改屏幕布局。

Disclaimer -- the above is what I do on MVC sites, I'm not an ember.js goto person so apologies in advance if I'm missing something obvious. 免责声明 - 以上是我在MVC网站上所做的事情,我不是一个ember.js转到此人,所以如果我遗漏了一些明显的东西,请提前道歉。

You could use this addon https://freshbooks.github.io/ember-responsive/ 您可以使用此插件https://freshbooks.github.io/ember-responsive/

and {{outlet 'sub1'}} to implement sub-templates {{outlet 'sub1'}}来实现子模板

I'm not sure if it supports Ember 1.11 (doubtful-- though you should upgrade!), but I highly recommend using https://github.com/runspired/flexi . 我不确定它是否支持Ember 1.11(可疑 - 虽然你应该升级!),但我强烈建议使用https://github.com/runspired/flexi

Instead of one template.hbs , Flexi allows you to define multiple 'layouts' (templates for different screen sizes) in a -layouts directory and takes care of the logic needed to switch between them. Flexi允许您在-layouts目录中定义多个“布局”(不同屏幕尺寸的template.hbs ,而不是一个template.hbs ,并负责在它们之间切换所需的逻辑。 It has many other features besides. 它还有许多其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM