我从emberjs.com博客教程视频开始进一步开发博客。 当我尝试保存注释以供ember-data rest适配器发布到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 票数: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'});
  });
});

这给出了一条路线

/ posts /:post_id / comments /:comment_id

我的博客文章提供了有关示例代码的 嵌套嵌套资源的更多信息。

  ask by dragonremo translate from so

未解决问题?本站智能推荐: