EmberJS 将记录放入存储
将记录放入存储
您还可以将记录推送到存储的缓存中,而无需从应用程序请求记录。push()方法有助于将记录放入存储。
Ember.Route.extend({
model: function() {
this.store.push(//do the logic)
}
});
在上面的代码中,push()方法将记录放入存储。
例子
<!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>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上述代码保存在 model_pushing_records.html 文件中
在浏览器中打开此HTML文件。