私が今やろうとしているのは、Ajax POST 経由でデータベースにデータを送信しようとしていることです。まず、私が持っている HTML フォームから始めます。
<script type="text/x-handlebars" id="project">
<div class="row">
<div class="span6">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="projectname">Project Name: </label>
<div class="controls">
{{!view App.TextFieldEmpty}}
<input type="text" id="projectname" required title="First Name is Required!" pattern="[A-z ]{10,}" placeholder="Enter Project Name"/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="projectdesc">Project Description:</label>
<div class="controls">
<textarea rows="3" id="projectdesc" placeholder="Enter Project Desc"
required="Description Required"></textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn" {{action 'createNew'}}>Add Project</button>
</div>
</div>
</form>
</div>
</div>
</script>
次に、私の ですApp.js
。Ember のガイドラインによると、最初にテンプレートをマップする必要がありますよね? だからここに私のものがありますrouter
:
App.Router.map(function() {
this.resource('project');
});
次に、データを挿入するテーブルは単純で、3 つのフィールドしかありません。id, projectname & projectdesc
.
App.Model = Ember.Object.extend({
});
App.Project = App.Model.extend({
id : null,
projectname : null,
projectdesc : null
});
さて、本題ですが、
App.ProjectController = Ember.ArrayController.extend({
actions : {
createNew : function() {
this.get('model').createNew();
}
}
});
App.ProjectRoute = Ember.Route.extend({
});
App.Project.reopenClass({
createNew : function() {
dataString = $("#project").serialize();
$.ajax({
type : "POST",
url : "http://ankur.local/users/createNewProject",
data : dataString,
dataType : "json",
success : function(data) {
alert("yes");
}
});
}
});
コンソールに次のエラーが表示されます。
Uncaught TypeError: Object [object Array] has no method 'createNew'
それはライン上にあるようです
this.get('model').createNew();
さらに、非 RESTful PHP バックエンドを使用しています。
モデルでメソッドを作成したと思いますが。さらに、私は GET をこのように正確に使用して動作しましたが、違いはモデルでメソッドを返すことでした。この場合、コントローラーからメソッドを呼び出さなければならないと思いました。どこで間違っているのでしょうか? 助けや提案をいただければ幸いです。