EmberJS-Models URL Conventions



Description

The REST adapter determines URL's that are communicates with model by name.

Action HTTP Verb URL
Find GET /URLrepr/123
Find All GET /URLrepr
Update PUT /URLrepr/123
Create POST /URLrepr
Delete DELETE /URLrepr/123

Syntax

 
store.find('post', 1).then(function(post){
   //do the stuff
});

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs URL Conventions</title>
      <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="fruits">
         <h1>Namespace is: <small>{{namespace}}</small></h1>
         <h2>Fruit names: </h2>
         {{#each fru in model}}
            <p><b>{{fru.title}}</b></p>
         {{/each}}
      </script>

      <script type="text/javascript">
         Fruits = Ember.Application.create();

         //Creating A Fixture Adapter
         Fruits.ApplicationAdapter = DS.FixtureAdapter.extend();

         Fruits.Router.map(function () {
            this.resource('fruits', { path: '/' });
         });

         Fruits.FruitsRoute = Ember.Route.extend({
            model: function () {
               return this.store.find('fru');
            }
         });

         Fruits.Fru = DS.Model.extend({
            //data model
            title: DS.attr('string')
         });

         //Endpoint Path Customization
         Fruits.FruitsAdapter = DS.RESTAdapter.extend({
            namespace: 'api'
         });

         //Attach Fixtures To The Model Class
         Fruits.Fru.FIXTURES = [{
            id: 1,
            title: 'Apple'},{
            id: 2,
            title: 'Orange'},{
            id: 3,
            title: 'Graps'
         }];
      </script>
   </body>
</html>

Output

Let's carry out the following steps to see how above code works −

  • Save above code in model_url_conven.htm file

  • Open this HTML file in a browser.

Advertisements