ember.js_如何設置基本的Ember.js應用

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396060.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396060.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396060.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

分數轉小數C語言,這是把小數轉換成分數的程序,可是輸入0.6666無限循環

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include int main(){double a;scanf("%lf", &a);輸入小數int b, c 0, d 0;double b1 a;do{b1 *10;b (int)b1;printf("%d\n", b);if(b%10!0){c;if(d>0){c d;d 0;}}else{d;}}while(d<5);printf("…

arm處理器的歷史及現狀

1 arm處理器的發展歷史 arm1 arm2 arm3 arm6 arm7 arm9 arm11 arm cortex 2 arm處理器現狀 arm cortex A a即application&#xff0c;即應用處理器&#xff0c;主要用在智能手機、平板電腦和服務器上。 arm cortex M m即mcu&#xff0c;即單片機上的處理器&#xff0c;它的特點…

Linq常用List操作總結,ForEach、分頁、交并集、去重、SelectMany等

1 /*2 以下圍繞Person類實現&#xff0c;Person類只有Name和Age兩個屬性3 一.List<T>排序4 1.1 List<T>提供了很多排序方法&#xff0c;sort(),Orderby(),OrderByDescending().5 */6 7 lstPerson lstPerson.OrderByDescending(x>x.Name).ToList(); //降序8 ls…

bool查詢原理 es_ES系列之原理copy_to用好了這么香

寫在前面Elasticsearch(以下簡稱ES)有個copy_to的功能&#xff0c;之前在一個項目中用到&#xff0c;感覺像是發現了一個神器。這個東西并不是像有些人說的是個語法糖。它用好了不但能提高檢索的效率&#xff0c;還可以簡化查詢語句。基本用法介紹直接上示例。先看看mapping&am…

加密算法—MD5、RSA、DES

最近因為要做一個加密的功能&#xff0c;簡單了解了一下加密算法&#xff0c;現在比較常用的有三個加密算法MD5加密算法、RSA加密算法、DES加密算法。 MD5加密算法 定義&#xff1a;MD5算法是將任意長度的“字節串”變換成一個128bit的大整數&#xff0c;并且它是一個不可逆的字…

隨機加密_隨機藝術和加密圣誕樹

隨機加密When I first learned how to code, one of my first tasks was setting up an SSH key so I could use encryption to securely connect to my friend’s Linux server.當我第一次學習如何編碼時&#xff0c;我的第一個任務是設置SSH密鑰&#xff0c;以便可以使用加密…

用c語言編寫一個2048 游戲,求c語言編寫的2048游戲代碼,盡量功能完善一些

正在編寫中&#xff0c;請稍后&#xff01;追答 : 代碼來了&#xff01;有點急&#xff0c;沒做界面。追答 : 2048_launcher。c&#xff1a;#include#include#includevoid main(){printf("正在啟動中&#xff0c;請稍后&#xff01;");Sleep(1000);system("bin\…

MySQL之數據庫對象查看工具mysqlshow

mysqlshow&#xff1a;數據庫對象查看工具&#xff0c;用來快速查找存在哪些數據庫、數據庫中的表、表中的列或索引。選項&#xff1a;--count 顯示數據庫和表的統計信息-k 顯示指定的表中的索引-i 顯示表的狀態信息不帶任何參數顯示所有數據庫[rootwww mys…

軟件工程分組

電子零售系統 陳仔祥 孟拓 陳庚 汪力 郭澳林 崔祥岑 劉校 肖宇 武清 胡圣陽轉載于:https://www.cnblogs.com/2231c/p/9960751.html

vnr光學識別怎么打開_干貨|指紋鎖的指紋識別模塊的前世今生,智能鎖的指紋識別到底有多智能?...

智能鎖現在也有很多叫法&#xff1a;指紋鎖、電子鎖。可見指紋識別是智能鎖的核心功能了&#xff0c;那我們今天來聊聊智能鎖的指紋識別模塊。指紋識別的歷史指紋識別認證的流程指紋識別技術的種類指紋識別的歷史早在2000多年前我國古代的人就將指紋用于簽訂合同和破案了&#…

使用Kakapo.js進行動態模擬

by zzarcon由zzarcon 使用Kakapo.js進行動態模擬 (Dynamic mocking with Kakapo.js) 3 months after the first commit, Kakapo.js reaches the first release and we are proud to announce that now it is ready to use. Let us introduce you Kakapo.首次提交3個月后&#…

android ble 實現自動連接,Android:自動重新連接BLE設備

經過多次試驗和磨難之后,這就是我最好讓Android自動連接的唯一用戶操作是首先選擇設備(如果使用設置菜單然后首先配對).您必須將配對事件捕獲到BroadcastReceiver中并執行BluetoothDevice.connectGatt()將autoconnect設置為true.然后當設備斷開連接時,調用gatt.connect().更新&…

萊斯 (less)

less中的變量 1、聲明變量&#xff1a;變量名&#xff1a;變量值 使用變量名&#xff1a;變量名 less中的變量類型 ①數字類1 10px ②字符串&#xff1a;無引號字符串red和有引號字符串"haha" ③顏色類red#000000 rgb() …

hackintosh黑蘋果_如何構建用于編碼的Hackintosh

hackintosh黑蘋果by Simon Waters西蒙沃特斯(Simon Waters) 如何構建用于編碼的Hackintosh (How to build a Hackintosh for coding) Let’s talk about Hackintosh-ing — the installation of Mac OS X on PC hardware.我們來談談Hackintosh-ing-在PC硬件上安裝Mac OSX。 I…

hide show vue 動畫_(Vue動效)7.Vue中動畫封裝

關鍵詞&#xff1a;動畫封裝——進行可復用一、如何封裝&#xff1f;1、使用&#xff1a;局部組件傳遞數據局部組件中使用JS動畫2、原理&#xff1a;將動畫效果完全第封裝在一個名為<fade>的組件中&#xff0c;今后如要復用&#xff0c;只需要復制有其組件名的部分&#…

android項目編譯命令行,命令行編譯Android項目

1. 生成R文件> aapt package -f -m -J ./gen -S res -M AndroidManifest.xml -I D:\android.jar-f 如果編譯生成的文件已經存在&#xff0c;強制覆蓋。-m 使生成的包的目錄存放在-J參數指定的目錄-J 指定生成的R.java 的輸出目錄路徑-S 指定res文件夾的路徑-I 指定某個版本平…

jQuery datepicker和jQuery validator 共用時bug

當我們給一個元素綁定一個datepick后又要對它用validator進行驗證時會發現驗證并沒有成功 因為當點擊該元素時候input彈出datepick的UI就已經失去了焦點它驗證的仍然是前一個值&#xff0c; 不過還好 datepick提供了onSelect 事件我們可以在這個事件觸發的時候重新把焦點在賦給…

《Python地理數據處理》——導讀

前言本書可以幫助你學習使用地理空間數據的基礎知識&#xff0c;主要是使用GDAL / OGR。當然&#xff0c;還有其他選擇&#xff0c;但其中一些都是建立在GDAL的基礎之上&#xff0c;所以如果你理解了本書中的內容&#xff0c;就可以很輕松地學習其他知識。這不是一本關于地理信…

記一次Java AES 加解密 對應C# AES加解密 的一波三折

最近在跟三方對接 對方采用AES加解密 作為一個資深neter Ctrl CV 是我最大的優點 所以我義正言辭的問他們要了demo java demo代碼&#xff1a; public class EncryptDecryptTool {private static final String defaultCharset "UTF-8";private static final String …

zemax評價函數編輯器_ZEMAX與光學設計案例:激光擴束系統詳細設計與公差分析(二)...

目前超過兩千人的光學與光學設計方面的微信公眾號&#xff0c;歡迎您的到來&#xff01;激光擴束系統公差分析ZEMAX與光學設計案例&#xff1a;激光擴束系統詳細設計與公差分析(二)作者&#xff1a;墨子川上10倍擴束系統在上篇已經設計好了&#xff0c;接下來就是進行系統的公差…