Ember数据嵌套资源URL

时间:2013-04-16 02:01:37

标签: ember.js ember-data

我正在进一步开发博客,从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>

有没有人遇到过类似的问题?如何为嵌套网址配置ember-data?

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题并使用了以下路线:

App.Router.map(function() {
  this.resource('posts');
  this.resource('post', { path: '/posts/:post_id' }, function() {
    this.resource('comments', function() {
   this.route('new');
      this.route('create');
    });
    this.route('comment', { path: 'comments/:comment_id'});
  });
});

这给出了像

这样的路线
  

/帖/:POST_ID /评论/:COMMENT_ID

我的博文中提供了有关ember nested resourcesexample code

的更多信息