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文件。
更多建議: