|
| 1 | +{{#template name="tutorials.whatsapp.meteor.step_01.md"}} |
| 2 | +We will start by creating the project’s folder structure, Meteor has a special behavior for certain folders: |
| 3 | + |
| 4 | +* client - these files will be available only in the client side. |
| 5 | +* server - these files will be available only in the server side. |
| 6 | +* public - these files will be available in the client, uses for assets, images, fonts, etc. |
| 7 | +* lib - any folder named lib (in any hierarchy) will be loaded first! |
| 8 | +* any other folder name will be included in both client and server and uses for code-sharing. |
| 9 | + |
| 10 | +So this will be our folder structure to the project: |
| 11 | + |
| 12 | +* client (client side with AngularJS and Ionic code) |
| 13 | +* - scripts |
| 14 | +* - templates |
| 15 | +* - styles |
| 16 | +* - index.html |
| 17 | +* server (server side code only) |
| 18 | +* public (assets, images) |
| 19 | +* lib (define methods and collections in order to make them available in both client and server) |
| 20 | + |
| 21 | +So let’s start by creating our first file - the `index.html` file - we will place in under the `client` folder: |
| 22 | + |
| 23 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.2"}} |
| 24 | + |
| 25 | +We used some ionic tags to achieve mobile style: |
| 26 | + |
| 27 | +* ion-nav-bar - Create a navigation bar in the page header. |
| 28 | +* ion-nav-view - This is a placeholder to the real content - AngularJS and ionic will put your content inside this tag automatically. |
| 29 | + |
| 30 | +Note that we only provide the `head` and `body` tags because Meteor takes care of the full contents of the HTML file, and any tag we will use here will be added to the Meteor’s main index.html file. |
| 31 | + |
| 32 | +This feature is really useful because we do not need to take care of including our files in `index.html` and keep it updated ourselves. |
| 33 | + |
| 34 | +Our next step is to create the AngularJS module and bootstrap it according to our platform. |
| 35 | +We will create a new file called `app.js`. |
| 36 | + |
| 37 | +This bootstrap file should be loaded first, because any other AngularJS code will depend on the module, so we need to put this file inside a folder called `lib`, so we will create a file in this path: `client/scripts/lib/app.js`. |
| 38 | + |
| 39 | +This file will contain an AngularJS module initialization with dependencies for `angular-meteor` and `ionic`. |
| 40 | + |
| 41 | +We will also check for the current platform (browser or mobile) and initialize the module according to the result: |
| 42 | + |
| 43 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.3"}} |
| 44 | + |
| 45 | +Our next step is to create the states and routes for the views. |
| 46 | + |
| 47 | +Our app uses Ionic to create 5 tabs: Favorites, Recents, Contacts, Chats, and Settings. |
| 48 | + |
| 49 | +We will define our routes and states with [angular-ui-router](https://atmospherejs.com/angularui/angular-ui-router) (which is included by ionic), and for the moment we will add the main page which is the `chats` tab: |
| 50 | + |
| 51 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.4"}} |
| 52 | + |
| 53 | +And this is the HTML template for the footer that included with the tabs view: |
| 54 | + |
| 55 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.5"}} |
| 56 | + |
| 57 | +Create the stub for the main page - the chats file: |
| 58 | + |
| 59 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.6"}} |
| 60 | + |
| 61 | +And this is what it looks at the moment, inside a browser: |
| 62 | + |
| 63 | +{{tutorialImage 'whatsapp-meteor' '1.png' 500}} |
| 64 | + |
| 65 | +If you want to view your app in a better way, with mobile layout, you can add a mobile platform as we described in the beginning of the step. I’ve added the iOS platform, and when we can run it inside a mobile emulator, and it looks like this: |
| 66 | + |
| 67 | +{{tutorialImage 'whatsapp-meteor' '2.png' 500}} |
| 68 | + |
| 69 | +Our next step includes creating basic views with some static data using `ionic` and `SASS`. |
| 70 | + |
| 71 | +First, let’s create an AngularJS controller that we will later connect to the chats view, we will call it `ChatsCtrl` and create a new file: |
| 72 | + |
| 73 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.8"}} |
| 74 | + |
| 75 | +We will use the controller with `conrollerAs` syntax, which means we won't put our variables on the `$scope` - we will use `this` context instead. |
| 76 | + |
| 77 | + |
| 78 | +Also, we injected the `$reactive` service, which is part of Angular-Meteor, and we used our `this` context and attached in to our `$scope`. |
| 79 | + |
| 80 | + |
| 81 | +`$reactive` will extend our controller with new functionality like creating Meteor helpers, subscription and use Autorun - all of these from our AngularJS controller context. |
| 82 | + |
| 83 | + |
| 84 | +Now we want to add some static data to this controller, we will use `moment` package to easily create time object, so let’s add it to the project using this command: |
| 85 | + |
| 86 | + $ meteor add momentjs:moment |
| 87 | + |
| 88 | +Now let’s add the static data, we will create a stub schema for chats and messages: |
| 89 | + |
| 90 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.10"}} |
| 91 | + |
| 92 | +Connect the chats view to the `ChatsCtrl`: |
| 93 | + |
| 94 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.11"}} |
| 95 | + |
| 96 | +Note that we used `controllerAs` syntax, so from now on, we will keep our controller variables on the `this` context, and we will used them with the `chats` prefix on the view. |
| 97 | + |
| 98 | +Modify the chats list view to use the stub data. |
| 99 | + |
| 100 | +We will use ionic’s tags to create a container with a list view (`ion-list` and `ion-item`), and add `ng-repeat` to iterate over the chats: |
| 101 | + |
| 102 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.12"}} |
| 103 | + |
| 104 | +And this is how is looks like: |
| 105 | + |
| 106 | +{{tutorialImage 'whatsapp-meteor' '3.png' 500}} |
| 107 | + |
| 108 | +You might notice that the dates are not formatted, so let’s create a simple AngularJS filter that use `moment` package to convert the date into formatted text, we will place it in a file named `client/scripts/filters/calendar.filter.js`: |
| 109 | + |
| 110 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.13"}} |
| 111 | + |
| 112 | +And let’s use it in our view: |
| 113 | + |
| 114 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.14"}} |
| 115 | + |
| 116 | +And this how it looks like now: |
| 117 | + |
| 118 | +{{tutorialImage 'whatsapp-meteor' '4.png' 500}} |
| 119 | + |
| 120 | +To add a delete button to our view, we will use `ion-option-button` which is a button that’s visible when we swipe over the list item! |
| 121 | + |
| 122 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.15"}} |
| 123 | + |
| 124 | +Implement the `remove(chat)` method inside our `ChatsCtrl`: |
| 125 | + |
| 126 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.16"}} |
| 127 | + |
| 128 | +And this is the result: |
| 129 | + |
| 130 | +{{tutorialImage 'whatsapp-meteor' '5.png' 500}} |
| 131 | + |
| 132 | +Now we want to add some styles and make some small CSS modifications to make it look more like WhatsApp. |
| 133 | + |
| 134 | +We want to use SASS in our project, so we need to add the sass package to our project: |
| 135 | + |
| 136 | + $ meteor add fourseven:scss |
| 137 | + |
| 138 | +And now we will create our first SASS file, we will place it under `client/styles/chats.scss`, and add some CSS rules: |
| 139 | + |
| 140 | +{{> DiffBox tutorialName="whatsapp-meteor-tutorial" step="1.18"}} |
| 141 | + |
| 142 | +And we are done with this view! It look just like WhatsApp! |
| 143 | + |
| 144 | +{{tutorialImage 'whatsapp-meteor' '6.png' 500}} |
| 145 | + |
| 146 | +{{/template}} |
0 commit comments