EmberJS 查找記錄

2018-01-03 14:00 更新

查找記錄

Ember.js有store對(duì)象的find方法,用于查找記錄。基于參數(shù),store對(duì)象使用find,findAll和findQuery方法,第一個(gè)參數(shù)是記錄類(lèi)型。方法的可選第二個(gè)參數(shù)確定所有記錄,單個(gè)記錄或查詢(xún)。

store.find();

在上面的代碼中,store.find()方法有助于在存儲(chǔ)中查找存儲(chǔ)的記錄。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Finding Records</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="search">
          <h1>Displaying details for the id value: {{info.id}}</h1>
          <p><b>info.id}}</b></p>
          <p><b>{{info.title}}</b></p> <p><b>{{info.content}}</b></p>
       </script>

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

          App.Router.map(function () {
             //search template
             this.route("search", { path: '/' });
          });

          //extending the Controller
          App.SearchController = Ember.Controller.extend();

          //extending the FixtureAdapter
          App.ApplicationAdapter = DS.FixtureAdapter.extend();

          App.SearchRoute = Ember.Route.extend({
              //INTEGRATING with the ROUTE'S MODEL HOOK
             model: function () {
                //return this.store.findAll('find'); To Find All Records

                //Finding A Single Record By Id Value
                return this.store.find('find',2);
             },
             //sets the model property of route
             setupController: function (controller, model) {
                controller.set('info', model);
             }
           });

          App.Find = DS.Model.extend({
             //data model
             title: DS.attr(),
             content: DS.attr()
          });

           //attach fixtures(sample data) to the model's class
          App.Find.FIXTURES = [{
             id: 1,
             title: 'Define Java',
             content: 'Java is pure object oriented language'},{
             id: 2,
             title: 'Define C',
             content: 'C is produre oriented language'
          }];
      </script>
   </body>
</html>

輸出

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

  • 將上述代碼保存在 model_str_find.html 文件中

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

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)