4

私は、この件に関する何らかのヒントを求めて Stack Overflow をグーグルで検索してきましたが、情報はせいぜい散らばっています。

Comment新しい子レコード ( ) を作成し、それを既存の親レコード ( ) に保存しようとしていますPost。私は Ember-Data ではなく Ember-Model を使用していますが、ヒントや指針をいただければ幸いです。

現時点では、新しい埋め込みの作成に成功していますCommentが、それは新しいレコードで作成された場合のみです。そう: Post

(子レコード) をPost適用するために、現在ロードされている (親レコード) をロード/取得するにはどうすればよいですか?Comments

別のコントローラー/モデルにアクセスするためにneeds:andthis.controllerForを使用して、コントローラーの依存関係を調べてきましたが、これらのものを意味のあるものに結び付けることができませんでした。this.modelForcontent

とにかく、これが私がこれを行う適切な方法につまずくことができるかもしれないことを期待して、アプリケーションコードを削ったものです...

ルート

    App.Router.map(function() {
      this.resource('post', { path: '/:post_id' }, function() {
        this.resource('comments', { path: '/comments'} );
        });
    });

他のすべてのリソースとルートを削除したのでApp.Post、 、App.PostIndex、が残りましApp.Commentsた。ここでのルートが問題だと思います。ロードされたPostレコードをCommentsルートで使用するメソッドを適切に実装していないと思います。

    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return App.Post.find();
      },

      setupController: function(controller, model) {  // I'm not certain if this
        controller.set('content', model);           // setupController is needed?
      }

    });

    App.PostRoute = Ember.Route.extend({
      model: function(params) {
        return App.Post.find(params.post_id);
      },

      setupcontroller: function( controller, model) { // again, unsure if this
          this.controllerFor('post').get('comments'); // is correct.
            controller.set('content', comments);
      }

    });

    App.CommentsRoute = Ember.Route.extend({
      afterModel: function() {
          this.set('post', this.modelFor('post'));
        },

      setupcontroller: function( controller, model) {
          this.controllerFor('post').get('comments');
            controller.set('content', comments);
        }

    });

コントローラ

App.CommentsController = Ember.ArrayController.extend({
  needs: "post",

  actions: {

     addComment: function() {
          var post = App.Post.create({
            title: 'static post title'
          });

              post.get('comments').create({
                message: 'static message'
            });

              post.save();

      }

  }

});

これは私の現在のコメントコントローラーでありPost、埋め込みで新しいを作成できますComment。を作成する多くの例を見つけて与えられましたCommentが、どれもうまくいかないようです。基本的にvar post = ...、現在ロードされているレコードとして を定義するのに苦労しています。試行錯誤の試みでさまざまなアプローチを実装しました。これまでのところ、私は試みました:

  • var post = App.Post.create();、未定義のプロパティを返します。これは、新しいレコードを作成するためです。ただし、これに関連して見たすべての例が彼らの記録をそのように定義していたので、私はそれに挑戦しました。

  • var post = this.get('post');、 undefined で「get」を呼び出すことはできません。Commentsコントローラーとコントローラーの両方で現在の投稿を定義するこの方法を使用してみましたPost

  • var post = this.get('controllers.post.content);、使用しているバックエンドから「循環エラー」を返します。

  • var post = App.Post.find();、 undefined で「get」を呼び出すことはできません。

  • var post = App.Post.find(1);、再び、未定義の「get」を呼び出すことはできません。これは人々が提供する繰り返しの例の1つであるため、試してみると思いました。私が使用するバックエンドは、各レコードに独自の ID を適用します。.find()メソッドで動的 ID 値を使用して、ロードしたばかりのモデルのみを取得できるかどうか、またはその方法が不明です。

ルートとコントローラーの依存関係を適切に設定していないと思いますか?

誰かが提案、関連するリンク、または修正を持っている場合、私は非常に感謝しています.

この 1 つの (一見単純な) 問題/ユース ケースは、この時点で機知に富んでいます。

4

1 に答える 1

1

これを試してください(ベータ2より前に動作します):

App.CommentsController = Ember.ArrayController.extend({
  actions: {
     addComment: function() {
        this.content.createRecord({
            message: 'static message'
        });   
     }
  }
});

Ember Data Beta 2 以降:

App.CommentsController = Ember.ArrayController.extend({
  needs: ["post"],
  actions: {
     addComment: function() {
        var post = this.get('controllers.post');
        var comment = this.get('store').createRecord('comment', {
            message: 'static message',
            post: post
        }); 
        comment.save().then(function() {
            post.addObject(comment);
            // You may or may not need to save your post, too. In my case my backend handles 
            // the inverses of relationships (if existing), so there's no need. We still need 
            // to do this for Ember, though

        });      
     }
  }
});
于 2013-09-20T18:34:10.677 に答える