ember.js

by Tracy Lee | ladyleet

特雷西·李(Tracy Lee)| Ladyleet

如何设置基本的Ember.js应用 (How to set up a Basic Ember.js app)

So, you want to test out Ember, eh? This article will walk through building a basic app.

所以,您想测试Ember,是吗? 本文将逐步构建一个基本的应用程序。

Here’s what we’ll do:

这是我们要做的:

  1. Set up ember-cli
    设置ember-cli
  2. Create a new application
    创建一个新的应用程序
  3. Use materalize-css for styling
    使用materalize-css进行样式设计
  4. Create components
    创建组件
  5. Cover basic use of Ember’s router
    涵盖Ember路由器的基本用法
  6. Explore the “each” helper for iterating over data
    探索“每个”助手来遍历数据

First things first, you should install ember-cli. Almost all applications are built with ember-cli. It’s very rare that you’ll find one that is not.

首先,您应该安装ember-cli。 几乎所有应用程序都是使用ember-cli构建的。 很少会发现一个不是的。

And here’s one major benefit of Ember and the Ember community — they rely on convention over configuration more heavily than Angular and React do. They use this as one of their strengths, making them a popular framework for companies who want to build large scale applications.

这是Ember和Ember社区的一个主要好处-与Angular和React相比,他们更加依赖约定而不是配置。 他们将其作为优势之一,使其成为想要构建大型应用程序的公司的流行框架。

Being conventional allows Ember to develop community standards such as the ember-cli-deploy story, a strong story around Ember Data, and the loads of contributions the community is able to make through the ember addon ecosystem. (check out emberaddons.com)

遵循常规可以使Ember制定社区标准,例如ember-cli-deploy故事,围绕Ember Data的精彩故事以及社区能够通过ember插件生态系统做出的大量贡献。 (查看emberaddons.com )

At the Ember.js website, you’ll find simple install instructions, and even a little quick start guide you can walk through!

在Ember.js网站上,您会找到简单的安装说明,甚至可以找到一些快速入门指南!

Go ahead and install ember-cli to get started:

继续并安装ember-cli以开始使用:

$ npm install -g ember-cli

创建一个新的应用程序 (Creating a new application)

This is as easy as 1–2–3! Simply ember new <project name> and an application will be generated for you.

就像1-2一样简单! 只需嵌入新的<project na me>,就会为您生成一个应用程序。

ember new yolobrolo

You’ll see ember-cli creating quite a few files.

您会看到ember-cli创建了很多文件。

Mainly, you should note that Ember has created:

主要,您应该注意Ember已创建:

  • application.hbs (handlebars, which is your html file)
    application.hbs(把手,这是您的html文件)
  • app.js
    app.js
  • router.js
    router.js
  • package.json
    package.json
  • bower.json
    bower.json
  • tests
    测试

Wahoo! Now, if you open up your IDE, you should see the structure of an Ember application.

哇! 现在,如果您打开IDE,则应该看到Ember应用程序的结构。

安装Materialize-CSS (Installing Materialize-CSS)

In case you were wondering, I love material design and materialize-css.

如果您想知道,我喜欢材料设计和materialize-css。

So, if you want to use the styles I usually use, go ahead run the following command.

因此,如果要使用我通常使用的样式,请继续运行以下命令。

npm install materialize-css

Then, add these lines to your index.html file

然后,将这些行添加到index.html文件中

<!-- Compiled and minified CSS -->  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<!--Import Google Icon Font-->      <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- Compiled and minified JavaScript -->  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/js/materialize.min.js"></script>

When done, kill your server and restart it. Your font should change to Roboto:

完成后,终止服务器并重新启动它。 您的字体应更改为Roboto:

创建组件 (Creating components)

Ember, like most JavaScript frameworks these days, loves components. So let’s create the component we need: a navigation bar that we can hook up the router to! We use the nav bar that materialize-css gives us.

像当今大多数JavaScript框架一样,Ember喜欢组件。 因此,让我们创建所需的组件:一个导航​​栏,可以将路由器连接到该导航栏! 我们使用materialize-css给我们的导航栏。

All you need to do to create a component is this:

创建组件所需要做的就是:

ember g component <component-name>

Make sure the name of your component has a dash in the name as this is the convention.

确保组件名称的名称中带有短划线,因为这是惯例。

Here are the files that ember-cli generates for me. It creates:

这是ember-cli为我生成的文件。 它创建:

  • component-name.hbs
    组件名称.hbs
  • component-name.js
    component-name.js
  • adds integration tests
    添加集成测试

This is what my pretty nav-bar looks like.

这就是我漂亮的导航栏的样子。

Here’s the default code if you like:

如果您愿意,这是默认代码:

<nav>    <div class="nav-wrapper">      <a href="#" class="brand-logo center">Logo</a>      <ul id="nav-mobile" class="left hide-on-med-and-down">        <li><a href="#">Videos</a></li>        <li><a href="#">About</a></li>      </ul>    </div>  </nav>

Anytime you need to reuse a piece of code over and over again, it’s always best to make it a component. :)

每当您需要一遍又一遍地重复使用一段代码时,始终最好使其成为组件。 :)

使用Ember的路由器 (Using Ember’s router)

I think I take Ember’s router for granted after playing around in Angular 2 so much.

我认为在Angular 2玩了这么多之后,我认为Ember的路由器是理所当然的。

Actually, I think I take routers for granted in general, but here is my friend Jay Phelps telling us why we should care.

实际上,我认为我一般认为路由器是理所当然的,但是我的朋友Jay Phelps告诉我们为什么我们应该关心。

Here’s a basic overview of how Ember’s router works.

这是Ember路由器工作原理的基本概述。

First things first, you should note there is a router.js file in which all of your routes are defined. Also, in your application.hbs file, there is {{outlet}} which outputs whatever you specify the router to.

首先,您应该注意有一个router.js文件,其中定义了所有路由。 另外,在application.hbs文件中,有{{outlet}}输出指定路由器的内容。

In my app, I want to create 2 simple routes — an about page and a videos page.

在我的应用中,我想创建2条简单的路线-“关于”页面和“视频”页面。

To create a new route you run this command in ember-cli.

要创建新路由,请在ember-cli中运行此命令。

ember g route <route-name>

Ember will then generate:

灰烬然后将生成:

  • your-route.js
    your-route.js
  • your-route.hbs
    您的route.hbs
  • update the router.js file
    更新router.js文件
  • create a unit test.
    创建一个单元测试。

You can see all the magic from the command line:

您可以从命令行查看所有魔术:

I love how the router.js file is automatically updated for me. You can even create nested routes from the command line. The Ember.js guides are pretty awesome and here is a link to everything the router can do.

我喜欢router.js文件自动为我更新的方式。 您甚至可以从命令行创建嵌套路由。 Ember.js指南非常棒,这是路由器可以执行的所有操作的链接 。

One thing I did in the screenshot below was define my default route. I did that by simply specifying the route path as /. Everything else was pre-generated for me with the CLI.

我在下面的屏幕截图中所做的一件事是定义了我的默认路由。 我只是通过将路由路径指定为/来做到这一点。 其他所有内容都是使用CLI为我预先生成的。

this.route(‘videos’, { path:’/’ });

配置Ember路由器的输出 (Configuring the output of Ember’s router)

Let’s explore the application.hbs file. This is where the router will output.

让我们探索application.hbs文件。 这是路由器将输出的位置。

Really, one of the only things I add into my application.hbs file is a navigation bar and footer. I create routes for everything else.

确实,我添加到application.hbs文件中的唯一内容之一是导航栏和页脚。 我为其他所有路线创建路线。

Currently my application.hbs file looks like this.

目前,我的application.hbs文件看起来像这样。

Now, going into my nav-bar component I’m going to get routes going for the about page route and the videos route.

现在,进入我的导航栏组件,我将获得关于网页页面路线和视频路线的路线。

Ember uses the {{link-to}} helper for transitions between routes.

Ember使用{{link-to}}帮助程序在路线之间进行转换。

Here’s what the syntax looks like:

语法如下所示:

{{#link-to ‘videos’}}Videos{{/link-to}}

The {{link-to}} helper replaces an <a> tag and is the way we transition between routes in Ember. All you need to do is specify the route in the parens as shown above. In Angular 2, the equivalent is the routerLink.

{{link-to}}帮助程序替换了<a>标记,这是我们在Ember中的各条路由之间进行转换的方式。 您需要做的就是在括号中指定路线,如上所示。 在Angular 2中,等效的是routerLink。

Here’s a screenshot of my entire nav-bar.

这是我整个导航栏的屏幕截图。

Now you know how to use the very basic functionality of the router!

现在您知道了如何使用路由器的基本功能!

使用每个助手迭代数据 (Iterating Over Data Using the Each Helper)

I have a video route, and I’d like to display a set of YouTube videos on the page. I’m going to create a simple video-card component that I will be iterating over and displaying on the video page.

我有一条视频路线,我想在页面上显示一组YouTube视频。 我将创建一个简单的视频卡组件,将对其进行迭代并显示在视频页面上。

This is what one video card looks like:

这是一个视频卡的外观:

Part of Ember’s beauty is all the helpers that allow you to do cool things in your app.

所有使您可以在应用程序中完成出色工作的助手,都是Ember的美丽之选。

Ember’s {{each}} helper is equivalent to the ng-repeat directive in Angular 1 and the *ngFor directive in Angular 2.

Ember的{{each}}辅助程序等效于Angular 1中的ng-repeat指令和Angular 2中的* ngFor指令。

Full ember docs on the each helper and helpers in general are here.

有关每个帮助程序和一般帮助程序的完整ember文档在这里 。

Here is what the code for one YouTube video displayed looks like:

这是显示的一部YouTube视频的代码如下所示:

<div class=”row”> <div class=”col s12 m6 l4"> <div class=”card-panel center-align”> <div class=”purple-text”> <p>Title</p> </div> <div class=”video-container”> <iframe width=”853" height=”480" src=”https://www.youtube.com/embed/peNV2yJRMLo?rel=0" frameborder=”0" allowfullscreen></iframe> </div> <div class=”purple-text”> With Taras Mankovski </div> </div> </div></div>

After laying it out, I realize that I want to iterate over 3 pieces of data — the title, the YouTube video link, and the person featured in the video.

进行布局后,我意识到我要遍历3个数据-标题,YouTube视频链接和视频中的人物。

So, I need to define my data in an array in my component.js file like such:

因此,我需要像这样在我的component.js文件的数组中定义数据:

model: [{ title: “Ember DND Helper”, people: “Taras Mankovski”, videoLink: “peNV2yJRMLo?rel=0” },{ title: “Dependency Injection in Angular 2”, people: “Patrick J. Stapleton”, videoLink: “46WovCX8i-I?rel=0” },{ title: “Angular CLI”, people: “Mike Brocchi”, videoLink: “BmZLpNRNnZo” },{ title: “Angular Material 2 Spelunking & Issue Submission”, people: “Ben Lesh”, videoLink: “3gNsyL7wpXU” }]});

Then, I can finally use the {{each}} helper to iterate over my data.

然后,我终于可以使用{{each}}帮助程序来遍历我的数据。

Wrap the content with the {{each}} helper as below, defining the model and your local variable:

使用{{each}}辅助函数将内容包装起来,如下所示,定义模型和局部变量:

{{#each model as |video|}} CONTENT {{/each}}

Then, take the pieces of content you’d like to be dynamic and replace it with handlebars and localVariable.x, like so:

然后,将您想动态化的内容片段替换为把手和localVariable.x,如下所示:

{{video.title}}
src="https://www.youtube.com/embed/{{video.videoLink}}"
{{video.people}}

Here’s what the code looks like when it’s all said and done:

说完这些,代码就是这样的:

<div class=”row”> {{#each model as |video|}} <div class=”col s12 m6 l4"> <div class=”card-panel center-align”> <div class=”purple-text”> <p>{{video.title}}</p> </div> <div class=”video-container”> <iframe width=”853" height=”480" src=”https://www.youtube.com/embed/{{video.videoLink}}" frameborder=”0" allowfullscreen></iframe> </div> <div class=”purple-text”> With {{video.people}} </div> </div> </div> {{/each}}</div>

Here’s the end result of using the {{each}} helper.

这是使用{{each}}帮助器的最终结果。

部署到Heroku (Deploying to Heroku)

Once upon a time there existed a man called tonycoco. And tonycoco made deploying ember apps to Heroku super easy. Here’s his github repo if you want to peep in on this.

曾几何时,有一个人叫做托尼科科。 而且tonycoco使将余烬应用程序部署到Heroku变得非常容易。 如果您想了解一下,这是他的github回购 。

First, you should have the Heroku Toolbelt installed and linked with your Heroku account.

首先,您应该已安装Heroku Toolbelt并与您的Heroku帐户关联。

Then, all you have to do to deploy to Heroku is commit your changes to master and push.

然后,部署到Heroku所要做的就是将更改提交给master和push。

$ heroku create — buildpack https://github.com/tonycoco/heroku-buildpack-ember-cli.git
$ git push heroku master

Wait for it to finish deploying completely.

等待它完全完成部署。

Go into your Heroku app dashboard. Update app to the name you want (to match your app).

进入您的Heroku应用仪表板 。 将应用更新为您想要的名称(以匹配您的应用)。

Now change the Heroku remote name to match new app name in your .git/config file.

现在更改Heroku远程名称以匹配您的.git / config文件中的新应用程序名称。

Then, git push heroku master again and you should be all set!

然后, git再次推送heroku master ,您应该一切准备就绪

In this case, this app deployed: http://yolobrolo-ember-1.herokuapp.com/

在这种情况下,该应用已部署:http: //yolobrolo-ember-1.herokuapp.com/

Yolo! Have fun with it. Hope you try out Ember and enjoy it.

olo! 玩得开心。 希望您尝试Ember并喜欢它。

看着我一步一步地建立 (Watch me build this step by step)

Oh also, for your viewing purposes, you can watch me build this on YouTube at yolobrolo.

哦,出于您的观看目的,您可以在yolobrolo上观看我在YouTube上进行构建 。

翻译自: https://www.freecodecamp.org/news/setting-up-a-basic-ember-js-app-c9323760c675/

ember.js

ember.js_如何设置基本的Ember.js应用相关推荐

  1. html设置根rem,经过js动态设置根元素的rem方案

    rem目前是响应式开发移动端一个很重要也是经常使用的一个元素,可是在网上看的各类文章都会超级懵逼.因此我在下面给出两个方案,也列举出使用方法,让你们一目了然.前提是设计稿以750为准.其中测试的设计稿 ...

  2. html加载js文件失败,firefox/chrome动态设置script加载js文件失败

    firefox,chrome等w3c浏览器下面,设置script标签的src来动态加载js文件时,有2中情况 1)如果script标签已经加载过js文件,那么重新设置为其他js文件的路径时,无法加载这 ...

  3. await原理 js_「速围」Node.js V14.3.0 发布支持顶级 Await 和 REPL 增强功能

    本周,Nodejs v14.3.0 发布.这个版本包括添加顶级 Await.REPL 增强等功能. REPL 增强 通过自动补全改进对 REPL 的预览支持,例如,下图中当输入 process.ver ...

  4. 设置 mock_Quasar 使用 Mock.js

    Quasar 使用 Mock.js 前言 之前我开发VUE的时候都是使用Element-UI,在Element-UI下有许多优秀的项目,比如著名的vue-element-admin. 不过Elemen ...

  5. pythonjs设置_在节点js中设置env变量并在python脚本中使用

    我正在node js app中设置一个env变量: process.env.data = "data-env"; 使用:print(os.environ["data&qu ...

  6. 实现js动态设置css样式,js动态设置全局样式主题色

    需求: 1.设置一个全局的样式 2. 样式是需要动态改变的,改变的内容是通过请求后台接口获取的,因此需要js能够对css进行改变 实现: 或许你可能不相信,代码只需要几行 js代码: // 设置主题色 ...

  7. js 设置html标签样式表,js怎么设置css样式?

    js怎么设置css样式?本篇文章就给大家介绍js设置css样式的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 1.直接设置style对象(内联样式) 使用JavaScript ...

  8. html js 设置样式,原生js设置样式 笔记:JS设置CSS样式的几种方式

    document(id名).style.backgroundColor = "ren" css里的属性有-的就去掉将首字母大写. js如何添加CSS样式中的class添加属性 用J ...

  9. 当时那个input都有值调用方法_miniUI input设置默认值,js获取年月注意事项,数据库nvl函数使用...

    2017-6-5周一,今天碰到的一个需求是:两税附征模块进入页面筛选时间默认值为当前月的上一个月,并根据筛选结果显示数据,我们用的框架为miniUI. 坑1: 默认值设置,刚刚接触miniUI,对里面 ...

最新文章

  1. outlook2010 打开总是提示“正在加载配置文件”
  2. 算法理解|从头开始理解梯度提升算法
  3. 关于 Group 的另一个函数
  4. 机器学习朴素贝叶斯算法+tkinter库界面实现好瓜坏西瓜分类
  5. ubuntu同时装有MXNet和Caffe框架
  6. mybatis快速入门(三)
  7. gRPC-go源码(2):ClientConn
  8. opencv 轮廓 缺陷检测
  9. 本地、服务器文件互传命令
  10. 反向传播算法BP公式推导
  11. LibreCAD的基本使用
  12. 大数据毕设/课设 - 基于大数据的公司业务监控大数据平台设计与实现
  13. 怎么有效提高淘宝店铺的转化率方法步骤
  14. PyBullet快速上手教程
  15. 从吴军的书《格局》总结出的37条为人方法论
  16. Java实现开发短信验证码的完整教程
  17. 生成带logo的二维码
  18. 图片转素描的工具汇总
  19. 运筹学基础,这个可以懂(上)
  20. Python tkinter教程-01:创建窗口

热门文章

  1. 百度、阿里、滴滴、新浪的面试心经总结,源码+原理+手写框架
  2. 交大c语言第一次作业答案,第一次作业答案(供参考)
  3. 大数据基础技术和应用
  4. 大项目之网上书城(八)——数据库大改添加图书
  5. wxpython实现界面跳转
  6. Zend Guard 7 , Zend Guard Loader处理PHP加密
  7. winfrom 点击按钮button弹框显示颜色集
  8. android 蓝牙各种UUID (转)
  9. Host 'xxx' is not allowed to connect to this MySQL server.
  10. Unity3D 与 objective-c 之间数据交互。iOS SDK接口封装Unity3D接口