├── .gitignore ├── .nodemonignore ├── Gruntfile.js ├── LICENSE ├── README.md ├── app ├── app.js ├── collections │ ├── base.js │ ├── repos.js │ └── users.js ├── controllers │ ├── home_controller.js │ ├── repos_controller.js │ └── users_controller.js ├── lib │ └── handlebarsHelpers.js ├── models │ ├── base.js │ ├── build.js │ ├── repo.js │ └── user.js ├── router.js ├── routes.js ├── templates │ ├── __layout.hbs │ ├── home │ │ └── index.hbs │ ├── repos │ │ ├── index.hbs │ │ └── show.hbs │ ├── user_repos_view.hbs │ └── users │ │ ├── index.hbs │ │ ├── show.hbs │ │ └── show_lazy.hbs └── views │ ├── app_view.js │ ├── base.js │ ├── home │ └── index.js │ ├── repos │ ├── index.js │ └── show.js │ ├── user_repos_view.js │ └── users │ ├── index.js │ └── show.js ├── assets ├── stylesheets │ ├── base_view.styl │ ├── index.styl │ └── vendor │ │ └── bootstrap.min.css └── vendor │ ├── es5-sham.js │ ├── es5-shim.js │ ├── jquery-1.9.1.min.js │ └── json2.js ├── config ├── default.yml ├── development.yml └── production.yml ├── index.js ├── package.json ├── public ├── favicon.ico └── images │ └── .gitkeep ├── server └── lib │ └── data_adapter.js └── test └── app ├── collections └── repos.test.js ├── models └── user.test.js └── views └── users_show_view.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | public/mergedAssets.js 3 | app/templates/compiledTemplates.js 4 | public/styles.css 5 | .DS_Store 6 | npm-debug.log 7 | config/runtime.json 8 | *.swp 9 | *.swo 10 | -------------------------------------------------------------------------------- /.nodemonignore: -------------------------------------------------------------------------------- 1 | /public/* 2 | /assets/* 3 | /**/compiledTemplates.js 4 | /node_modules/* 5 | *.swp 6 | *.swo 7 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | var stylesheetsDir = 'assets/stylesheets'; 4 | var rendrDir = 'node_modules/rendr'; 5 | var rendrHandlebarsDir = 'node_modules/rendr-handlebars'; 6 | var rendrModulesDir = rendrDir + '/node_modules'; 7 | 8 | module.exports = function(grunt) { 9 | // Project configuration. 10 | grunt.initConfig({ 11 | pkg: grunt.file.readJSON('package.json'), 12 | 13 | stylus: { 14 | compile: { 15 | options: { 16 | paths: [stylesheetsDir], 17 | 'include css': true 18 | }, 19 | files: { 20 | 'public/styles.css': stylesheetsDir + '/index.styl' 21 | } 22 | } 23 | }, 24 | 25 | handlebars: { 26 | compile: { 27 | options: { 28 | namespace: false, 29 | commonjs: true, 30 | processName: function(filename) { 31 | return filename.replace('app/templates/', '').replace('.hbs', ''); 32 | } 33 | }, 34 | src: "app/templates/**/*.hbs", 35 | dest: "app/templates/compiledTemplates.js", 36 | filter: function(filepath) { 37 | var filename = path.basename(filepath); 38 | // Exclude files that begin with '__' from being sent to the client, 39 | // i.e. __layout.hbs. 40 | return filename.slice(0, 2) !== '__'; 41 | } 42 | } 43 | }, 44 | 45 | watch: { 46 | scripts: { 47 | files: 'app/**/*.js', 48 | tasks: ['rendr_stitch'], 49 | options: { 50 | interrupt: true 51 | } 52 | }, 53 | templates: { 54 | files: 'app/**/*.hbs', 55 | tasks: ['handlebars'], 56 | options: { 57 | interrupt: true 58 | } 59 | }, 60 | stylesheets: { 61 | files: [stylesheetsDir + '/**/*.styl', stylesheetsDir + '/**/*.css'], 62 | tasks: ['stylus'], 63 | options: { 64 | interrupt: true 65 | } 66 | } 67 | }, 68 | 69 | rendr_stitch: { 70 | compile: { 71 | options: { 72 | dependencies: [ 73 | 'assets/vendor/**/*.js' 74 | ], 75 | npmDependencies: { 76 | underscore: '../rendr/node_modules/underscore/underscore.js', 77 | backbone: '../rendr/node_modules/backbone/backbone.js', 78 | handlebars: '../rendr-handlebars/node_modules/handlebars/dist/handlebars.runtime.js', 79 | async: '../rendr/node_modules/async/lib/async.js' 80 | }, 81 | aliases: [ 82 | {from: rendrDir + '/client', to: 'rendr/client'}, 83 | {from: rendrDir + '/shared', to: 'rendr/shared'}, 84 | {from: rendrHandlebarsDir, to: 'rendr-handlebars'}, 85 | {from: rendrHandlebarsDir + '/shared', to: 'rendr-handlebars/shared'} 86 | ] 87 | }, 88 | files: [{ 89 | dest: 'public/mergedAssets.js', 90 | src: [ 91 | 'app/**/*.js', 92 | rendrDir + '/client/**/*.js', 93 | rendrDir + '/shared/**/*.js', 94 | rendrHandlebarsDir + '/index.js', 95 | rendrHandlebarsDir + '/shared/*.js' 96 | ] 97 | }] 98 | } 99 | } 100 | }); 101 | 102 | grunt.loadNpmTasks('grunt-contrib-stylus'); 103 | grunt.loadNpmTasks('grunt-contrib-watch'); 104 | grunt.loadNpmTasks('grunt-contrib-handlebars'); 105 | grunt.loadNpmTasks('grunt-rendr-stitch'); 106 | 107 | grunt.registerTask('runNode', function () { 108 | grunt.util.spawn({ 109 | cmd: 'node', 110 | args: ['./node_modules/nodemon/nodemon.js', '--debug', 'index.js'], 111 | opts: { 112 | stdio: 'inherit' 113 | } 114 | }, function () { 115 | grunt.fail.fatal(new Error("nodemon quit")); 116 | }); 117 | }); 118 | 119 | 120 | grunt.registerTask('compile', ['handlebars', 'rendr_stitch', 'stylus']); 121 | 122 | // Run the server and watch for file changes 123 | grunt.registerTask('server', ['runNode', 'compile', 'watch']); 124 | 125 | // Default task(s). 126 | grunt.registerTask('default', ['compile']); 127 | }; 128 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Airbnb 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Note: 2 | 3 | ## This repo has been deprecated in favor of the 4 | [rendr-examples](https://github.com/rendrjs/rendr-examples) repo, which is 5 | easier to keep up-to-date. 6 | 7 | 8 | ----------- 9 | 10 | # Rendr App Template 11 | ## GitHub Browser 12 | 13 | The purpose of this little app is to demonstrate one way of using Rendr to build a web app that runs on both the client and the server. 14 | 15 | ![Screenshot](http://cl.ly/image/062d3S2D1Y38/Screen%20Shot%202013-04-09%20at%203.14.31%20PM.png) 16 | 17 | ## Running the example 18 | 19 | First, make sure to have Node >= 0.8.0 [installed on your system](http://nodejs.org/). Also, make sure to have `grunt-cli` installed globally. 20 | 21 | $ npm install -g grunt-cli 22 | 23 | If you see an error on startup that looks [like this](https://github.com/airbnb/rendr-app-template/issues/2), then you may need to un-install a global copy of `grunt`: 24 | 25 | $ npm uninstall -g grunt 26 | 27 | Clone this repo to a local directory and run `npm install` to install dependencies: 28 | 29 | $ git clone git@github.com:airbnb/rendr-app-template.git 30 | $ cd rendr-app-template 31 | $ npm install 32 | 33 | Then, use `grunt server` to start up the web server. Grunt will recompile and restart the server when files change. 34 | 35 | $ grunt server 36 | Running "bgShell:runNode" (bgShell) task 37 | 38 | Running "handlebars:compile" (handlebars) task 39 | File "app/templates/compiledTemplates.js" created. 40 | 41 | Running "rendr_stitch:compile" (rendr_stitch) task 42 | 4 Apr 09:58:02 - [nodemon] v0.7.2 43 | 4 Apr 09:58:02 - [nodemon] watching: /Users/spike1/code/rendr-app-template 44 | 4 Apr 09:58:02 - [nodemon] starting `node index.js` 45 | 4 Apr 09:58:02 - [nodemon] reading ignore list 46 | File "public/mergedAssets.js" created. 47 | 48 | Running "stylus:compile" (stylus) task 49 | File public/styles.css created. 50 | server pid 87338 listening on port 3030 in development mode 51 | 52 | Running "watch" task 53 | Waiting... 54 | 55 | Now, pull up the app in your web browser. It defaults to port `3030`. 56 | 57 | $ open http://localhost:3030 58 | 59 | You can choose a different port by passing the `PORT` environment variable: 60 | 61 | $ PORT=80 grunt server 62 | 63 | ### GitHub API rate limit 64 | 65 | GitHub [rate limits](http://developer.github.com/v3/#rate-limiting) unauthenticated requests to its public API to 60 requests per hour per IP. This should be enough for just playing with the sample app, but if you pull it down and start developing off it you may run up against the rate limit. 66 | 67 | If this happens to you, you can supply your GitHub creds for HTTP Basic Auth using the BASIC_AUTH environment variable. **Be very, very careful with this!** It means you will be typing your GitHub credentials in plain text, which will be saved to your Bash history and may be intercepted by other programs. If you do this, immediately change your password before and afterwards. This should only be necessary if you're developing on the app and need to keep refreshing the page. 68 | 69 | $ BASIC_AUTH=githubusername:githubpassword grunt server 70 | 71 | **You've been warned.** Your best bet may be to alter the project to read from your favorite RESTful API. 72 | 73 | ## Getting Started With Rendr 74 | 75 | It's worthwhile to read the first [blog post](http://nerds.airbnb.com/weve-launched-our-first-nodejs-app-to-product), which has some background on Rendr and its *raison d'être*. 76 | 77 | This basic Rendr app looks like a hybrid between a standard client-side MVC Backbone.js app and an Express app, with a little Rails convention thrown in. 78 | 79 | Check out the directory structure: 80 | 81 | |- app/ 82 | |--- collections/ 83 | |--- controllers/ 84 | |--- models/ 85 | |--- templates/ 86 | |--- views/ 87 | |--- app.js 88 | |--- router.js 89 | |--- routes.js 90 | |- assets/ 91 | |- config/ 92 | |- public/ 93 | |- server/ 94 | 95 | **Note**: I want to stress that this is just one way to build an app using Rendr. I hope it can evolve to support a number of different app configurations, with the shared premise that the components should be able to run on either side of the wire. For example, the full-on client-side MVC model isn't appropriate for all types of apps. Sometimes it's more appropriate to load HTML fragments over the wire, also known as PJAX. Rendr apps should be able to support this as well. 96 | 97 | ## CommonJS using Stitch 98 | 99 | Node.js uses the CommonJS module pattern, and using a tool called [Stitch](https://github.com/sstephenson/stitch), we can emulate it in the browser. This looks familiar in Node.js: 100 | 101 | ```js 102 | var User = require('app/models/user'); 103 | ``` 104 | Using Stitch, we can use the same `require()` function in the browser. This allows us to focus on application logic, not packaging modules separately for client and server. 105 | 106 | In Node.js, you can also use `require()` to load submodules within NPM models. For example, we could load Rendr's base view in order to extend it to create a view for our app. 107 | 108 | ```js 109 | var BaseView = require('rendr/shared/base/view'); 110 | ``` 111 | 112 | Because of a trick in the way we do Stitch packaging, this module path works in the browser as well. 113 | 114 | ## Routes file 115 | 116 | ```js 117 | // app/routes.js 118 | module.exports = function(match) { 119 | match('', 'home#index'); 120 | match('repos', 'repos#index'); 121 | match('repos/:owner/:name', 'repos#show'); 122 | match('users' , 'users#index'); 123 | match('users/:login', 'users#show'); 124 | }; 125 | 126 | ``` 127 | 128 | ## Controllers 129 | 130 | A controller is a simple JavaScript object, where each property is a controller action. Keep in mind that controllers are executed on both the client and the server. Thus, they are an abstraction whose sole responsibility is to specify which data is needed to render the view, and which view to render. 131 | 132 | On the server, controllers are executed in response to a request to the Express server, and are used to render the initial page of HTML. On the client, controllers are executed in response to `pushState` events as the user navigates the app. 133 | 134 | Here is a very simple controller: 135 | 136 | ```js 137 | // app/controllers/home_controller.js 138 | module.exports = { 139 | index: function(params, callback) { 140 | callback(null, 'home_index_view'); 141 | } 142 | }; 143 | 144 | ``` 145 | 146 | Every action gets called with two arguments: `params` and `callback`. The `params` object contains both route params and query string params. `callback` is called to kick off view rendering. It has this signature: 147 | 148 | ```js 149 | function(err, viewName, viewData) {} 150 | ``` 151 | 152 | ### `err` 153 | Following the Node.js convention, the first argument to the callback is `err`. We'll pass null here because we're not fetching any data, but if we were, that's how we'd communicate a fetching error. 154 | 155 | ### `viewName` 156 | This is a string identifier of a view, used by the router to find the view class, i.e.: 157 | 158 | ```js 159 | require('app/views/' + viewName); 160 | ``` 161 | 162 | ### `viewData` (optional) 163 | An object to pass to the view constructor. This is how we pass data to the view. 164 | 165 | All our `index` action above really does is specify a view class. This is the simple case -- no data fetching, just synchronous view rendering. 166 | 167 | It gets more interesting when we decide to fetch some data. Check out the `repos_controller` below: 168 | 169 | ```js 170 | // app/controllers/repos_controller.js 171 | module.exports = { 172 | // ... 173 | 174 | show: function(params, callback) { 175 | var spec = { 176 | model: {model: 'Repo', params: params} 177 | }; 178 | this.app.fetch(spec, function(err, result) { 179 | callback(err, 'repos_show_view', result); 180 | }); 181 | } 182 | }; 183 | 184 | ``` 185 | 186 | You see here that we call `this.app.fetch()` to fetch our Repo model. Our controller actions are executed in the context of the router, so we have a few properties and methods available, one of which is `this.app`. This is the instance of our application's App context, which is a sublcass of `rendr/base/app`, which itself is a subclass of `Backbone.Model`. You'll see that we inject `app` into every model, view, collection, and controller; this is how we maintain app context throughout our app. 187 | 188 | You see here that we call `callback` with the `err` that comes from `this.app.fetch()`, the view class name, and the `result` of the fetch. `result` in this case is an object with a single `model` property, which is our instance of the `Repo` model. 189 | 190 | `this.app.fetch()` does a few nice things for us; it fetches models or collections in parallel, handles errors, does caching, and most importantly, provides a way to boostrap the data fetched on the server in a way that is accessible by the client-side on first render. 191 | 192 | ## Views 193 | 194 | A Rendr view is a subclass of `Backbone.View` with some additional methods added to support client-server rendering, plus methods that make it easier to manage the view lifecycle. 195 | 196 | Creating your own view should look familiar if you've used Backbone: 197 | 198 | ```js 199 | // app/views/home_index_view.js 200 | var BaseView = require('./base_view'); 201 | 202 | module.exports = BaseView.extend({ 203 | className: 'home_index_view', 204 | 205 | events: { 206 | 'click p': 'handleClick', 207 | }, 208 | 209 | handleClick: function() {…} 210 | }); 211 | module.exports.id = 'HomeIndexView'; 212 | ``` 213 | 214 | You can add `className`, `tagName`, `events`, and all of the other `Backbone.View` properties you know and love. 215 | 216 | We set the property `id` on the view constructor to aid in the view hydration process. More on that later. 217 | 218 | Our views, just like all of the code in the `app/` directory, are executed in both the client and the server, but of course certain behaviors are only relevant in the client. The `events` hash is ignored by the server, as well as any DOM-related event handlers. 219 | 220 | Notice there's no `render()` method or `template` property specified in the view. The philosophy here is that sensible defaults and convention over configuration should allow you to skip all the typical boilerplate when creating views. The `render()` method should be the same for all your views; all it does is mash up the template with some data to generate HTML, and insert that HTML into the DOM element. 221 | 222 | Now, because we're not using a DOM to render our views, we must make sure that the view returns all its HTML as a string. On the server, `view.getHtml()` is called, which returns the view's outer HTML, including wrapper element. This is then handed to Express, which wraps the page with a layout and sends the full HTML page to the client. Behind the scenes, `view.getHtml()` calls `view.getInnerHtml()` for the inner HTML of the view, not including wrapping element, and then constructs the wrapping element based on the `tagName`, `className`, etc. properties, and the key-value pairs of HTML attributes returned by `view.getAttributes()`, which allows you to pass custom attributes to the outer element. 223 | 224 | On the client, `view.render()` is called, which updates the view's DOM element with the HTML returned from `view.getInnerHtml()`. By default, Backbone will create the wrapper DOM element on its own. We make sure to also set any custom HTML attributes in `view.getAttributes()` on the element. 225 | 226 | ### The view lifecycle 227 | 228 | 229 | A common need is to run some initialization code that touches the DOM after render, for things like jQuery sliders, special event handling, etc. Rather than overriding the `render()` method, use `postRender()`. The `postRender()` method is executed for every view once after rending, including after initial pageload. 230 | 231 | ```js 232 | // app/views/home_index_view.js 233 | var BaseView = require('./base_view'); 234 | 235 | module.exports = BaseView.extend({ 236 | className: 'home_index_view', 237 | 238 | postRender: function() { 239 | this.$('.slider').slider(); 240 | } 241 | }); 242 | module.exports.id = 'HomeIndexView'; 243 | ``` 244 | 245 | If you have a need to customize the way your views generate HTML, there are a few specific methods you can override. 246 | 247 | #### getTemplateName() 248 | 249 | By default, `getTemplateName()` returns the underscored version of the view constructor's `id` property; so in our case, `home_index_view`. It will also look for `options.template_name`, which is useful for initialing views to use a certain template. The view will look in `app/templates` for the value returned by this function. 250 | 251 | #### getTemplate() 252 | 253 | If `getTemplateName()` isn't enough, you can override `getTemplate()` to return a function that takes a single `data` argument and returns HTML: 254 | 255 | ```js 256 | function(data) { 257 | ... 258 | return html; 259 | } 260 | ``` 261 | 262 | This HTML is used to populate the view's inner HTML; that is, not including the wrapper element, which you can specify on the view itself using `tagName`, `className`, and `id`. 263 | 264 | #### getInnerHtml() 265 | 266 | If you're building some sort of composite view that doesn't utilize a simple template, override `getInnerHtml()`. This is useful for tabbed views, collection views, etc. 267 | 268 | #### getHtml() 269 | 270 | You probably shouldn't ever need to override this; by default it just combines the HTML returned by `getInnerHtml()` and the HTML attributes returned by `getAttributes()` to produce an outer HTML string. 271 | 272 | ## The view hierarchy 273 | 274 | Rendr provides a Handlebars helper `{{view}}` that allows you to declaratively nest your views, creating a view hierarchy that you can traverse in your JavaScript. Check out [`app/templates/users/show.hbs`](https://github.com/airbnb/rendr-app-template/blob/master/app/templates/users/show.hbs) and [`app/views/users/show.js`](https://github.com/airbnb/rendr-app-template/blob/master/app/views/users/show.js) for an example: 275 | 276 | ```html 277 | 278 | ... 279 | 280 |
281 | {{view "user_repos_view" collection=repos}} 282 |
283 | 284 |
285 | ... 286 |
287 | ``` 288 | 289 | You see that we use the `{{view}}` helper with an argument that indicates which view to be rendered. We can pass data into the view using [Handlebars' hash arguments](http://handlebarsjs.com/expressions.html). Anything you pass as hash arguments will be pass to the subview's constructor and be accessible as `this.options` within the subview. There are a few special options you can pass to a view: `model` or `collection` can be used to directly pass a model or collection instance to a subview. The options `model_name` + `model_id` or `collection_name` + `collection_params` can be used in conjunction with `lazy="true"` to lazily fetch models or collections; more on that later. 290 | 291 | Now, from within the `users/show` view, we can access any child views using the `this.childViews` array. A good way to debug and get a feel for this in the browser is to drill down into the global `App` property, which is your instance of `BaseApp`. From `App` you can access other parts of your application. `App.router` is your instance of `ClientRouter`, and it has a number of properties that you can inspect. One of these is `App.router.currentView`, which will always point to the current main view for a page. For example, if you are viewing wycats' page in our app, [http://localhost:3030/users/wycats](http://localhost:3030/users/wycats), `currentView` will be an instance of `users/show`: 292 | 293 | App.router.currentView 294 | => child {render: function, cid: "view434", model: child, options: Object, $el: p.fn.p.init[1]…} 295 | 296 | From there, we can find our child `user_repos_view` view: 297 | 298 | App.router.currentView.childViews 299 | => [child] 300 | 301 | App.router.currentView.childViews[0] 302 | => child {render: function, cid: "view436", options: Object, $el: p.fn.p.init[1], el: div.user_repos_view…} 303 | 304 | Check out its collection property, which is the instance of `Repos` which we fetched in the controller and passed down in the `{{view}}` helper: 305 | 306 | App.router.currentView.childViews[0].collection 307 | => child {options: Object, app: child, params: Object, meta: Object, length: 30…} 308 | 309 | You can nest subviews *ad infinitum*. Our `user_repos_view` has an empty `childViews` array now, but we could add some subviews if we found it useful for organizing our codebase, or keeping things DRY. 310 | 311 | App.router.currentView.childViews[0].childViews 312 | => [] 313 | 314 | Views also have a `parentView` property, which will be non-null unless they are a top-level view. 315 | 316 | App.router.currentView.childViews[0].parentView === App.router.currentView 317 | => true 318 | 319 | App.router.currentView.parentView 320 | => null 321 | 322 | ## Lazy-loading data for views 323 | 324 | So far, our [`users#show` action](https://github.com/airbnb/rendr-app-template/blob/master/app/controllers/users_controller.js#L11) pulls down both a `User` model and a `Repos` collection for that model. If we were to navigate from `users#index` to `users#show`, we already have that user model cached in memory (because we fetched it in order to render the list), but we have to make a roundtrip to the server to fetch the `Repos`, which aren't part of the `User` attributes. This means that instead of immediately rendering the `users/show` view, we wait for the `Repos` API call to finish. But what if instead we want to lazy-load the `Repos` so we can render that view immediately for a better user experience? 325 | 326 | We can achieve this by lazy-loading models or collections in our subviews. Check out the `users#show_lazy` action, which demonstrates this approach: 327 | 328 | ```js 329 | // app/controllers/users_controller.js 330 | module.exports = { 331 | // ... 332 | 333 | show_lazy: function(params, callback) { 334 | var spec = { 335 | model: {model: 'User', params: params} 336 | }; 337 | this.app.fetch(spec, function(err, result) { 338 | if (err) return callback(err); 339 | // Extend the hash of options we pass to the view's constructor 340 | // to include the `template_name` option, which will be used 341 | // to look up the template file. This is a convenience so we 342 | // don't have to create a separate view class. 343 | _.extend(result, { 344 | template_name: 'users/show_lazy' 345 | }); 346 | callback(err, 'users/show', result); 347 | }); 348 | } 349 | } 350 | ``` 351 | The first thing to notice is that in our fetch `spec`, we only specify the `User` model, leaving out the `Repos` collection. Then, we tell the view to use a different template than the default. We do this by passing in a `template_name` property to the view's options, which is passed to its constructor. We extend the `result` object to have this; the third argument to our `callback` is an object that's passed to the view's constructor. We could have also created a separate view class in JavaScript for this, to match our new template. 352 | 353 | Here's the `users/show_lazy` template, abbreviated: 354 | 355 | ```html 356 | 357 | ... 358 | 359 |
360 | {{view "user_repos_view" collection_name="Repos" param_name="login" param_value=login lazy="true"}} 361 |
362 | 363 |
364 | ... 365 |
366 | ``` 367 | 368 | So, the only difference to our original `users/show` template is that instead of passing `collection=repos` to our `user_repos_view` subview, we pass `collection_name="Repos" param_name="login" param_value=login lazy="true"`. When fetching collections, we specify params, which are used to fetch and cache the models for that collection. We quote all of these arguments except for `param_value=login`; quoted arguments are passed in as string literals, and unquoted arguments are references to variables that are available in the current Handlebars scope. `login` is one of the attributes of a `User` model, which gets passed into the template. The `lazy="true"` tells the view that it needs to fetch (or find a cached version of) the specified model or collection. 369 | 370 | We can see this at play in our app if we add a route in our [`app/routes.js`](https://github.com/airbnb/rendr-app-template/blob/master/app/routes.js#L7) file that routes `users_lazy/:login` to `users#show_lazy`, and change our [`app/templates/users_index_view.hbs`](https://github.com/airbnb/rendr-app-template/blob/master/app/templates/users_index_view.hbs#L6) to link to `/users_lazy/{{login}}`. 371 | 372 | Now, if we click from the list of users on `users#index`, you'll see the page gets rendered immediately, and the repos are rendered once the API call finishes. If you click back and forward in your browser, you see it's cached. 373 | 374 | ## Templates 375 | 376 | So far, Rendr just supports Handlebars templates, but it should be possible to make this interchangeable. For now, place your templates in `app/templates` with a name that matches the underscorized view's identifier and file extension of `.hbs`. So, the view with an identifier of `HomeIndexView` will look for a template at `app/templates/home_index_view.hbs`. 377 | 378 | 379 | ## Interacting with a RESTful API 380 | 381 | 382 | ## Assets 383 | 384 | In this example we use [Grunt](https://github.com/gruntjs/grunt) to manage asset compilation. We compile JavaScripts using [Stitch](https://github.com/sstephenson/stitch) and stylesheets using [Stylus](https://github.com/learnboost/stylus). Check out `Gruntfile.js` in the root directory of this repo for details. 385 | 386 | 387 | ## License 388 | 389 | MIT 390 | -------------------------------------------------------------------------------- /app/app.js: -------------------------------------------------------------------------------- 1 | var BaseApp = require('rendr/shared/app') 2 | , handlebarsHelpers = require('./lib/handlebarsHelpers'); 3 | 4 | /** 5 | * Extend the `BaseApp` class, adding any custom methods or overrides. 6 | */ 7 | module.exports = BaseApp.extend({ 8 | 9 | /** 10 | * Client and server. 11 | * 12 | * `postInitialize` is called on app initialize, both on the client and server. 13 | * On the server, an app is instantiated once for each request, and in the 14 | * client, it's instantiated once on page load. 15 | * 16 | * This is a good place to initialize any code that needs to be available to 17 | * app on both client and server. 18 | */ 19 | postInitialize: function() { 20 | /** 21 | * Register our Handlebars helpers. 22 | * 23 | * `this.templateAdapter` is, by default, the `rendr-handlebars` module. 24 | * It has a `registerHelpers` method, which allows us to register helper 25 | * modules that can be used on both client & server. 26 | */ 27 | this.templateAdapter.registerHelpers(handlebarsHelpers); 28 | }, 29 | 30 | /** 31 | * Client-side only. 32 | * 33 | * `start` is called at the bottom of `__layout.hbs`. Calling this kicks off 34 | * the router and initializes the application. 35 | * 36 | * Override this method (remembering to call the superclass' `start` method!) 37 | * in order to do things like bind events to the router, as shown below. 38 | */ 39 | start: function() { 40 | // Show a loading indicator when the app is fetching. 41 | this.router.on('action:start', function() { this.set({loading: true}); }, this); 42 | this.router.on('action:end', function() { this.set({loading: false}); }, this); 43 | 44 | // Call 'super'. 45 | BaseApp.prototype.start.call(this); 46 | } 47 | 48 | }); 49 | -------------------------------------------------------------------------------- /app/collections/base.js: -------------------------------------------------------------------------------- 1 | var RendrBase = require('rendr/shared/base/collection'); 2 | 3 | module.exports = RendrBase.extend({}); 4 | -------------------------------------------------------------------------------- /app/collections/repos.js: -------------------------------------------------------------------------------- 1 | var Repo = require('../models/repo') 2 | , Base = require('./base'); 3 | 4 | module.exports = Base.extend({ 5 | model: Repo, 6 | url: function() { 7 | if (this.params.user != null) { 8 | return '/users/:user/repos'; 9 | } else { 10 | return '/repositories'; 11 | } 12 | } 13 | }); 14 | module.exports.id = 'Repos'; 15 | -------------------------------------------------------------------------------- /app/collections/users.js: -------------------------------------------------------------------------------- 1 | var User = require('../models/user') 2 | , Base = require('./base'); 3 | 4 | module.exports = Base.extend({ 5 | model: User, 6 | url: '/users' 7 | }); 8 | module.exports.id = 'Users'; 9 | -------------------------------------------------------------------------------- /app/controllers/home_controller.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | index: function(params, callback) { 3 | callback(); 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /app/controllers/repos_controller.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | index: function(params, callback) { 3 | var spec = { 4 | collection: {collection: 'Repos', params: params} 5 | }; 6 | this.app.fetch(spec, function(err, result) { 7 | callback(err, result); 8 | }); 9 | }, 10 | 11 | show: function(params, callback) { 12 | var spec = { 13 | model: {model: 'Repo', params: params, ensureKeys: ['language', 'watchers_count']}, 14 | build: {model: 'Build', params: params} 15 | }; 16 | this.app.fetch(spec, function(err, result) { 17 | callback(err, result); 18 | }); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /app/controllers/users_controller.js: -------------------------------------------------------------------------------- 1 | var _ = require('underscore'); 2 | 3 | module.exports = { 4 | index: function(params, callback) { 5 | var spec = { 6 | collection: {collection: 'Users', params: params} 7 | }; 8 | this.app.fetch(spec, function(err, result) { 9 | callback(err, result); 10 | }); 11 | }, 12 | 13 | show: function(params, callback) { 14 | var spec = { 15 | model: {model: 'User', params: params}, 16 | repos: {collection: 'Repos', params: {user: params.login}} 17 | }; 18 | this.app.fetch(spec, function(err, result) { 19 | callback(err, result); 20 | }); 21 | }, 22 | 23 | // This is the same as `show`, but it doesn't fetch the Repos. Instead, 24 | // the `users_show_lazy_view` template specifies `lazy=true` on its 25 | // subview. We have both here for demonstration purposes. 26 | show_lazy: function(params, callback) { 27 | var spec = { 28 | model: {model: 'User', params: params} 29 | }; 30 | this.app.fetch(spec, function(err, result) { 31 | if (err) return callback(err); 32 | // Extend the hash of options we pass to the view's constructor 33 | // to include the `template_name` option, which will be used 34 | // to look up the template file. This is a convenience so we 35 | // don't have to create a separate view class. 36 | _.extend(result, { 37 | template_name: 'users/show_lazy' 38 | }); 39 | callback(err, 'users/show', result); 40 | }); 41 | } 42 | }; 43 | -------------------------------------------------------------------------------- /app/lib/handlebarsHelpers.js: -------------------------------------------------------------------------------- 1 | /** 2 | * We inject the Handlebars instance, because this module doesn't know where 3 | * the actual Handlebars instance will come from. 4 | */ 5 | module.exports = function(Handlebars) { 6 | return { 7 | copyright: function(year) { 8 | return new Handlebars.SafeString("©" + year); 9 | } 10 | }; 11 | }; 12 | -------------------------------------------------------------------------------- /app/models/base.js: -------------------------------------------------------------------------------- 1 | var RendrBase = require('rendr/shared/base/model'); 2 | 3 | module.exports = RendrBase.extend({}); 4 | -------------------------------------------------------------------------------- /app/models/build.js: -------------------------------------------------------------------------------- 1 | var Base = require('./base'); 2 | 3 | module.exports = Base.extend({ 4 | url: '/repos/:owner/:name', 5 | api: 'travis-ci' 6 | }); 7 | module.exports.id = 'Build'; 8 | -------------------------------------------------------------------------------- /app/models/repo.js: -------------------------------------------------------------------------------- 1 | var Base = require('./base'); 2 | 3 | module.exports = Base.extend({ 4 | url: '/repos/:owner/:name', 5 | idAttribute: 'name' 6 | }); 7 | module.exports.id = 'Repo'; 8 | -------------------------------------------------------------------------------- /app/models/user.js: -------------------------------------------------------------------------------- 1 | var Base = require('./base'); 2 | 3 | module.exports = Base.extend({ 4 | url: '/users/:login', 5 | idAttribute: 'login' 6 | }); 7 | module.exports.id = 'User'; 8 | -------------------------------------------------------------------------------- /app/router.js: -------------------------------------------------------------------------------- 1 | var BaseClientRouter = require('rendr/client/router'); 2 | 3 | var Router = module.exports = function Router(options) { 4 | BaseClientRouter.call(this, options); 5 | }; 6 | 7 | /** 8 | * Cross-platform inheritance, taking advantage of `es5shim` for IE. 9 | */ 10 | Router.prototype = Object.create(BaseClientRouter.prototype); 11 | Router.prototype.constructor = BaseClientRouter; 12 | 13 | Router.prototype.postInitialize = function() { 14 | this.on('action:start', this.trackImpression, this); 15 | }; 16 | 17 | Router.prototype.trackImpression = function() { 18 | if (window._gaq) { 19 | _gaq.push(['_trackPageview']); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /app/routes.js: -------------------------------------------------------------------------------- 1 | module.exports = function(match) { 2 | match('', 'home#index'); 3 | match('repos', 'repos#index'); 4 | match('repos/:owner/:name', 'repos#show'); 5 | match('users' , 'users#index'); 6 | match('users/:login', 'users#show'); 7 | match('users_lazy/:login', 'users#show_lazy'); 8 | }; 9 | -------------------------------------------------------------------------------- /app/templates/__layout.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Rendr Example App 7 | 8 | 9 | 10 | 11 | 12 | 27 | 28 |
29 | {{{body}}} 30 |
31 | 32 | 33 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/templates/home/index.hbs: -------------------------------------------------------------------------------- 1 |

Wecome to GitHub Browser!

2 |

This is a little app that demonstrates how to use Rendr by consuming GitHub's public Api.

3 |

Check out Repos or Users.

4 | 5 |

{{copyright "2013"}}

6 | -------------------------------------------------------------------------------- /app/templates/repos/index.hbs: -------------------------------------------------------------------------------- 1 |

Repos

2 | 3 | 10 | -------------------------------------------------------------------------------- /app/templates/repos/show.hbs: -------------------------------------------------------------------------------- 1 | {{owner.login}} / {{name}}
2 | 3 | 4 | {{#if build.last_build_id}} 5 |
6 |

Latest TravisCI build

7 | {{/if}} 8 | 9 |

Stats

10 |
11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 |
Description{{description}}
Language{{language}}
Watchers{{watchers_count}}
Forks{{forks_count}}
Open Issues{{open_issues_count}}
34 |
35 |
36 | -------------------------------------------------------------------------------- /app/templates/user_repos_view.hbs: -------------------------------------------------------------------------------- 1 |

Repos

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | {{#each models}} 12 | 13 | 14 | 15 | 16 | 17 | {{/each}} 18 | 19 |
NameWatchersForks
{{name}}{{watchers_count}}{{forks_count}}
20 | -------------------------------------------------------------------------------- /app/templates/users/index.hbs: -------------------------------------------------------------------------------- 1 |

Users

2 | 3 | 10 | -------------------------------------------------------------------------------- /app/templates/users/show.hbs: -------------------------------------------------------------------------------- 1 | {{login}} ({{public_repos}} public repos) 2 | 3 |
4 | 5 |
6 |
7 | {{view "user_repos_view" collection=repos}} 8 |
9 | 10 |
11 |

Info

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
Location{{location}}
Blog{{blog}}
23 |
24 |
25 | -------------------------------------------------------------------------------- /app/templates/users/show_lazy.hbs: -------------------------------------------------------------------------------- 1 | {{login}} ({{public_repos}} public repos) 2 | 3 |
4 | 5 |
6 |
7 | {{view "user_repos_view" collection_name="Repos" param_name="login" param_value=login lazy="true"}} 8 |
9 | 10 |
11 |

Info

12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
Location{{location}}
Blog{{blog}}
23 |
24 |
25 | -------------------------------------------------------------------------------- /app/views/app_view.js: -------------------------------------------------------------------------------- 1 | var BaseAppView = require('rendr/shared/base/app_view'); 2 | 3 | var $body = $('body'); 4 | 5 | module.exports = BaseAppView.extend({ 6 | postInitialize: function() { 7 | this.app.on('change:loading', function(app, loading) { 8 | $body.toggleClass('loading', loading); 9 | }, this); 10 | } 11 | }); 12 | -------------------------------------------------------------------------------- /app/views/base.js: -------------------------------------------------------------------------------- 1 | var RendrView = require('rendr/shared/base/view'); 2 | 3 | // Create a base view, for adding common extensions to our 4 | // application's views. 5 | module.exports = RendrView.extend({}); 6 | -------------------------------------------------------------------------------- /app/views/home/index.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('../base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'home_index_view' 5 | }); 6 | module.exports.id = 'home/index'; 7 | -------------------------------------------------------------------------------- /app/views/repos/index.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('../base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'repos_index_view' 5 | }); 6 | module.exports.id = 'repos/index'; 7 | -------------------------------------------------------------------------------- /app/views/repos/show.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('../base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'repos_show_view', 5 | 6 | getTemplateData: function() { 7 | var data = BaseView.prototype.getTemplateData.call(this); 8 | data.build = this.options.build.toJSON(); 9 | return data; 10 | } 11 | }); 12 | module.exports.id = 'repos/show'; 13 | -------------------------------------------------------------------------------- /app/views/user_repos_view.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('./base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'user_repos_view' 5 | }); 6 | module.exports.id = 'user_repos_view'; 7 | -------------------------------------------------------------------------------- /app/views/users/index.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('../base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'users_index_view' 5 | }); 6 | module.exports.id = 'users/index'; 7 | -------------------------------------------------------------------------------- /app/views/users/show.js: -------------------------------------------------------------------------------- 1 | var BaseView = require('../base'); 2 | 3 | module.exports = BaseView.extend({ 4 | className: 'users_show_view', 5 | 6 | getTemplateData: function() { 7 | var data = BaseView.prototype.getTemplateData.call(this); 8 | data.repos = this.options.repos; 9 | return data; 10 | } 11 | }); 12 | module.exports.id = 'users/show'; 13 | -------------------------------------------------------------------------------- /assets/stylesheets/base_view.styl: -------------------------------------------------------------------------------- 1 | [data-view] 2 | .loading 3 | opacity: 0.5 4 | -------------------------------------------------------------------------------- /assets/stylesheets/index.styl: -------------------------------------------------------------------------------- 1 | @import 'vendor/bootstrap.min.css' 2 | @import 'base_view' 3 | 4 | body 5 | background-color: whitesmoke 6 | padding-top: 50px 7 | 8 | &.loading 9 | .loading-indicator 10 | display: block 11 | 12 | .loading-indicator 13 | display: none 14 | float: right 15 | color: white 16 | line-height: 40px 17 | -------------------------------------------------------------------------------- /assets/vendor/es5-sham.js: -------------------------------------------------------------------------------- 1 | // Copyright 2009-2012 by contributors, MIT License 2 | // vim: ts=4 sts=4 sw=4 expandtab 3 | 4 | // Module systems magic dance 5 | (function (definition) { 6 | // RequireJS 7 | if (typeof define == "function") { 8 | define(definition); 9 | // YUI3 10 | } else if (typeof YUI == "function") { 11 | YUI.add("es5-sham", definition); 12 | // CommonJS and