EmberJS 使用夾具

2018-01-03 13:53 更新

使用夾具

夾具用于開(kāi)發(fā)客戶端Web應(yīng)用程序。您可以將夾具附加到保存樣本數(shù)據(jù)的模型類(lèi)。

App.ApplicationAdapter = DS.FixtureAdapter;

DS.Model.extend({
   //define data model
});

FIXTURES =[ //define sample data  ];

在上面的代碼中,F(xiàn)IXTURE保存要在瀏覽器上顯示的樣本數(shù)據(jù)。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Using Fixtures</title>
      <!-- CDN's-->
      <script src="/attachements/w3c/handlebars.min.js"></script>
      <script src="/attachements/w3c/jquery-2.1.3.min.js"></script>
      <script src="/attachements/w3c/ember.min.js"></script>
      <script src="/attachements/w3c/ember-template-compiler.js"></script>
      <script src="/attachements/w3c/ember.debug.js"></script>
      <script src="/attachements/w3c/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="index">
         <h2>Players Name:</h2>
         {{#each person in controller}}
            <p>{{person.id}}: <b>{{person.name}}</b></p>
         {{/each}}
      </script>

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

         //The store cache of all records available in an application
         App.Store = DS.Store.extend({
            //adapter translating requested records into the appropriate calls
            adapter: 'DS.FixtureAdapter'
         });

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

         //Define Your Model
         App.Person = DS.Model.extend({
            name: DS.attr('string')
         });

         App.IndexRoute = Ember.Route.extend({
            //index route
            model: function() {
               //return the person details
               return this.store.find('person');
            }
         });

         //Attach Fixtures To The Model Class
          App.Person.FIXTURES = [
             {id: 1, name: 'Virat'},
           {id: 2, name: 'Raina'},
          {id: 3, name: 'Dhoni'}];
     </script>
   </body>
</html>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將以上代碼保存在 fixture_model.html 文件中

  • 在瀏覽器中打開(kāi)此HTML文件。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)