- EmberJS - Home
- EmberJS - Overview
- EmberJS - Installation
- EmberJS - Core Concepts
- Creating and Running Application
- EmberJS - Object Model
- EmberJS - Router
- EmberJS - Templates
- EmberJS - Components
- EmberJS - Models
- EmberJS - Managing Dependencies
- EmberJS - Application Concerns
- EmberJS - Configuring Ember.js
- EmberJS - Ember Inspector
EmberJS-Persisting Records
Persisting Records
The Ember.js Records data are persisted on a per-instance basis. The DS.Model takes the save() on any instance to make a network request.
store.createRecord('route', {
//declare properties
route.save();
});
In the above code, route.save() helps to records the data.
Example
<!DOCTYPE html>
<html>
<head>
<title>Emberjs Persisting Records</title>
<!-- CDN's-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
<script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
<script src="https://builds.emberjs.com/release/ember.debug.js"></script>
<script src="https://builds.emberjs.com/beta/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>
Output
Let's carry out the following steps to see how above code works −
Save above code in persist_model.htm file
Open this HTML file in a browser.
Advertisements