EmberJS 持久记录
2018-01-03 14:08 更新
持久记录
Ember.js记录数据在每个实例的基础上持久化。DS.Model对任何实例使用save()发出网络请求。
store.createRecord('route', { //declare properties route.save(); });
在上面的代码中,route.save()有助于记录数据。
例子
<!DOCTYPE html> <html> <head> <title>Emberjs Persisting 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="fruits"> <h1>Enter Fruit Names to Add</h1> {{input type="text" value=newTitle action="createFru"}} {{#each fru in model}} {{fru.title}} {{/each}} </script> <script type="text/javascript"> Fruits = Ember.Application.create(); Fruits.ApplicationAdapter = DS.FixtureAdapter.extend(); Fruits.Router.map(function () { this.resource('fruits', { path: '/' }); }); Fruits.FruitsRoute = Ember.Route.extend({ model: function () { return this.store.find('fru'); } }); Fruits.Fru = DS.Model.extend({ //data model title: DS.attr('string') }); //attach fixtures(sample data) to the model's class Fruits.Fru.FIXTURES = [{ id: 1, title: 'Apple' }]; Fruits.FruitsController = Ember.ArrayController.extend({ actions: { createFru: function () { // Get the fru title set by the "New Todo" text field var title = this.get('newTitle'); // Create the new 'fru'(fruit) model var fru = this.store.createRecord('fru', { title: title }); // Save the new model fru.save(); } } }); </script> </body> </html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将以上代码保存在 persist_model.html 文件中
在浏览器中打开此HTML文件。
以上内容是否对您有帮助:
更多建议: