Skip to content

Instantly share code, notes, and snippets.

@alemohamad
Last active August 29, 2015 14:21
Show Gist options
  • Save alemohamad/be0022e6a35706c8c54a to your computer and use it in GitHub Desktop.
Save alemohamad/be0022e6a35706c8c54a to your computer and use it in GitHub Desktop.

Ember.js

Ember.js

http://emberjs.com/

Ember.js Resources

App files

Routes

// app.js
App = Ember.Application.create();

App.Router.map(function() {
  this.route("about");
  this.route("collections");
  this.resource("exhibits");
  this.route("notes");
});

Nested Routes

// app.js

// this will be instead of "exhibits" resource route

this.resource("exhibits", function(){
  this.resource("exhibit", { path: "/:exhibit_id" });
});

Templates (Handlebars)

// index.html

<script type="text/x-handlebars">
<div>{{outlet}}</div>
</script>

<script type="text/x-handlebars" data-template-name="index">
<h1>Index</h1>
</script>

<script type="text/x-handlebars" id="about">
<h1>About Section</h1>
</script>

Link-to helper

{{#link-to "routeName" class="some-class"}}Some link{{/link-to}}

To load another template inside the template that is calling that template, we should add the this keyword:

{{#link-to "nestedRouteName" this}}Template inside a template{{/link-to}}

For this to work, we have to add an {{outlet}} in the parent template.

Component

<script type="text/x-handlebars" id="components/my-component">
  <h1>My Component</h1>
  <span class="myClass">Hello, World</span>
</script>

// display the component
<script type="text/x-handlebars" id="myTemplate">
  {{my-component}}
</script>

Example (with bind-attr and yield):

<script type="text/x-handlebars" id="components/single-collection">
  <h3>{{title}}</h3>
  <img {{bind-attr src=image}}>
  {{yield}}
</script>

<script type="text/x-handlebars" id="collections">
  <section>
    {{#each}}
      {{#single-collection title=title image=image}}
        {{copy}}
      {{/single-collection}}
    {{/each}}
  </section>
</script>

Customize the wrapper of the component:

// app.js

App.SingleCollectionComponent = Ember.Component.extend({
  tagName: "article",
  classNames: ["collectionArticleClass cf"]
});

Models

Controllers

Route Controllers

// app.js

App.CollectionsRoute = Ember.Route.extend({
  model: function(){
    return []; // JSON
  }
});

Object Controllers

// app.js

App.ExhibitController = Ember.ObjectController.extend({
  exhibitTitle: function() {
    return this.get("title") + " by " + this.get("artist_name");
  }.property("artist_name", "title")
});

Array Controllers

// app.js

App.ExhibitsController = App.ArrayController.extend({
  totalExhibits: function() {
    return this.get("model.length");
  }.property("@each")
});

Web Components

About Web Components

Load external JSON as Model Data

// app.js

App.CollectionsRoute = Ember.Route.extend({
  model: function(){
    return $.getJSON("js/exhibits.json").then(function(data) {
      return data.exhibits;
    });
  }
});

Ember Data (local storage)

// app.js

App.Note = DS.Model.extend({
  copy: DS.attr()
});

App.NotesRoute = Ember.Route.extend({
  model: functon() {
    return this.store.find("note");
  }
});

App.NotesController = Ember.ArrayController.extend({
  actions: {
    newNote: function() {
      var copy = this.get("noteText");
      if (!copy) {
        return false;
      }
      
      var note = this.store.createRecord("note", {
        copy: copy
      });
      
      this.set("noteText", "");
      note.save();
    }
  }
});

App.ApplicationAdapter = DS.LSAdapter.extend({
  namespace: "samocaNotes"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment