Hello there , I am trying to post data as well as to save it in Database , but It seems that I am stuck very hard this time ,So please help me to get over this ,Thanks in advance
My Code in app.js
var App = Ember.Application.create();
App.Router.map(function() {
this.resource('about');
this.resource('persons', function() {
this.resource('data', { path: ':person_id' });
});
});
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
url: 'http://port/path_to_save_data'
})
});
App.PersonsRoute = Ember.Route.extend({
model: function() {
return this.store.find('person');
}
});
App.Person = DS.Model.extend({
name: DS.attr('string'),
file: DS.attr('string')
});
App.DataController = Ember.ObjectController.extend({
isEditing: false,
edit: function() {
this.set('isEditing', true);
},
doneEditing: function() {
this.set('isEditing', false);
var record = this.get('store').createRecord('persons', {
name: 'Rails is Omakase',
file: 'Lorem ipsum'
});
record.save();
}
});
Complete Code in index.html is
<html>
<head>
<meta charset="utf-8">
<title>Ember Starter Kit</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet">
</head>
<body>
<script type="text/x-handlebars" id="blogger">
<h2>Here in Blogger</h2>
</script>
<script type="text/x-handlebars">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li>{{#link-to 'persons'}}Person{{/link-to}}</li>
<li>{{#link-to 'about'}}About{{/link-to}}</li>
</ul>
</div>
</div>
{{outlet}}
</script>
<script type="text/x-handlebars" id="persons">
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<table class='table'>
<thead>
<tr><th>Recent Posts </th></tr>
</thead>
{{#each model}}
<tr><td>
{{#link-to 'data' this}} {{name}}{{/link-to}}
{{file}}
</td></tr>
{{/each}}
</table>
</div>
<div class="span9">
{{outlet}}
</div>
</div>
</div>
</script>
<script type="text/x-handlebars" id="data">
{{#if isEditing}}
{{partial 'data/edit'}}
<button {{action 'doneEditing'}}>Done</button>
{{else}}
<button {{action 'edit'}}>Edit</button>
{{/if}}
<div class='below-the-fold'>
<p></p>
<p>{{name}}</p>
<p>{{file}}</p>
</div>
</script>
<script type="text/x-handlebars" id="data/_edit">
<p class="text-warning">Please select a post</p>
<p>{{view Ember.TextField valueBinding = 'name'}}</p>
<p>{{view Ember.TextField valueBinding = 'file'}}</p>
</script>
<script type="text/x-handlebars" id="about">
<p>in about route</p>
</script>
<script src="js/libs/jquery-1.10.2.js"></script>
<script src="js/libs/handlebars-1.1.2.js"></script>
<script src="js/libs/ember-1.5.1.js"></script>
<script src="js/bower_components/ember-data/ember-data.js"></script>
<script src="js/app.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/showdown/0.3.1/showdown.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.1.0/moment.min.js"></script>
<!-- to activate the test runner, add the "?test" query string parameter -->
<script src="tests/runner.js"></script>
</body>
</html>
And the code that I am trying to save data from is
test_saveData:
pattern: /test/ember/persons/{id}
defaults: { _controller: TestDefaultDirTestBundle:Default:save }
Action for this route is :
public function saveAction(Request $request)
{
$em = $this->getDoctrine()->getEntityManager();
if ($request->isMethod('POST'))
{
$jsonData = $request->get('jsonData');
$serializer = $this->get('serializer');
$persons = $serializer->deserialize($jsonData, 'TestDefaultDir\TestBundle\WSClasses\Persons', 'json');
foreach ($persons->persons as $p)
{
$person = new Person();
$person->setName($p->name);
$person->setFile($p->file);
$em->persist($person);
$em->flush();
}
$em->flush();
}
}
Please let me know where I am lacking,