codecamp

EmberJS 模型URL约定

描述

REST适配器确定按名称与模型通信的URL。

ActionHTTP VerbURL
FindGET/URLrepr/123
Find AllGET/URLrepr
UpdatePUT/URLrepr/123
CreatePOST/URLrepr
DeleteDELETE/URLrepr/123

语句

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

例子

<!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>

输出

让我们执行以下步骤,看看上面的代码如何工作:

  • 将上面的代码保存在model_url_conven.html文件中

  • 在浏览器中打开此HTML文件。

温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }