您還可以將記錄推送到存儲(chǔ)的緩存中,而無(wú)需從應(yīng)用程序請(qǐng)求記錄。push()方法有助于將記錄放入存儲(chǔ)。
Ember.Route.extend({ model: function() { this.store.push(//do the logic) } });
在上面的代碼中,push()方法將記錄放入存儲(chǔ)。
<!DOCTYPE html> <html> <head> <title>Emberjs Pushing Records Into The Store</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"> {{outlet}} </script> <script type="text/x-handlebars" data-template-name="index"> <h3>Fruit Name and Color: </h3> {{#each item in model}} <p>Color of the fruit: {{item.color}}<br> Name of the fruit: {{item.name}}</p> {{/each}} </script> <script type="text/javascript"> App = Ember.Application.create(); App.Color = DS.Model.extend({ //data model color: DS.attr(), name: DS.attr() }); App.ApplicationRoute = Ember.Route.extend({ model: function() { //push() method pushes records into the store this.store.push('color', { id: 1, color: "Red", name: "Apple" }); this.store.push('color', { id: 2, color: "Yellow", name: "Mango" }); //returns the all stored data return this.store.all('color'); } }); </script> </body> </html>
讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:
將上述代碼保存在 model_pushing_records.html 文件中
在瀏覽器中打開(kāi)此HTML文件。
更多建議: