我正在进一步开发博客,从 emberjs.com 博客教程视频开始。当我尝试保存评论以服务 ember-data 时,其余适配器正在发布,
http://localhost/blog/comments
但它应该http://localhost/blog//posts/1/comments
是嵌套资源。下面是我的代码。
var App = Ember.Application.create({
LOG_TRANSITIONS: true,
ready: function() {
this.set('Router.enableLogging', true);
}
});
App.Router.map(function() {
this.resource('about');
this.resource('posts', function() {
this.resource('post', {
path: ':post_id'
});
this.route('new');
});
});
App.IndexRoute = Ember.Route.extend({
redirect: function() {
this.transitionTo('posts');
}
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return App.Post.find();
}
});
App.Comment = DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string'),
text: DS.attr('string'),
createdAt: DS.attr('date')
});
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
intro: DS.attr('string'),
extended: DS.attr('string'),
publishedAt: DS.attr('date'),
comments: DS.hasMany('App.Comment', {embedded:'always'})
});
App.Store = DS.Store.extend({
revision: 12,
adapter: DS.RESTAdapter.extend({
url: 'http://localhost/blog'
})
});
App.CommentNewController = Ember.ObjectController.extend({
needs: ["post"],
saveComment: function(params) {
console.log("Dinesh is coding");
console.log("iname:" + params.iname);
console.log("text:" + params.text);
console.log("email:" + params.email);
//params.set('name', params.iname);
var post = this.get('controllers.post').get('model');
debugger;
var comments = post.get('comments');
comments.createRecord({
name:params.iname,
text:params.text,
email:params.email,
post_id:post.get('id')
});
//this.get('controllers.comments').pushObject(params);
this.get('store').commit();
},
content: {}
// }
});
App.CommentNewView = Ember.View.extend({
template: Ember.Handlebars.compile(comment_new_html)
// controller: App.CommentController
});
我的评论新观点:
<form {{action "saveComment" content on="submit" validation="true"}}>
<p>{{view Ember.TextField valueBinding="iname" id="iname" placeholder="Name" required="true"}}<br /></p>
<p>{{view Ember.TextField valueBinding="email" id="email" placeholder="Email" required="true"}}<br /></p>
<p>{{view Ember.TextArea valueBinding="text" id="text" placeholder="Comment" }}</p>
<button type="submit" class="btn">Save Comment</button>
</form>
有没有人遇到过类似的问题?如何为嵌套 url 配置 ember-data?