EmberJS 查找记录

2018-01-03 14:00 更新

查找记录

Ember.js有store对象的find方法,用于查找记录。基于参数,store对象使用find,findAll和findQuery方法,第一个参数是记录类型。方法的可选第二个参数确定所有记录,单个记录或查询。

store.find();

在上面的代码中,store.find()方法有助于在存储中查找存储的记录。

例子

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

输出

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

  • 将上述代码保存在 model_str_find.html 文件中

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

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号