1

私は現在、ember.js、ember-resource、couchdbを使用してアプリケーションに取り組んでいます。

私のデータモデルには、ネストされたリソースがいくつかあります。

MyApp.Task = Ember.Resource.define({
url: '/tasks',
schema: {
    id:          String,
    _rev:        String,
    title:       String,
    description: String,
    comments: {
        type: Ember.ResourceCollection,
        itemType: 'MyApp.Comment',
        nested: true
    }
});

MyApp.Comment = Ember.Resource.define({
    url: null,
    schema: {
        created: Date,
        start: Number,
        end: Number,
        text: String
    }
});

最初にデータベースに「完全な」モデル、つまり空のコメントモデルを持つタスクを提供する限り、すべてが正常に機能します。この場合、タスクにコメントを追加できます。

    var newComment = MyApp.Comment.create({ created: created, start: start, end: end, text: text });
    var comments = task.get('comments');
    comments.pushObject(newComment);

ただし、初期taskデータにはが埋め込まれていないため、ネストされたコメントのをプログラムでcomments作成する必要があります。Ember.ResourceCollection

私はさまざまなアプローチを試し、ember-resource仕様でいくつかのコードを見つけようとしましたが、どの試みもうまくいきませんでした。

私の最新のアプローチは

var comments = this.get('comments');
if (!comments) {
    comments = Ember.ResourceCollection.create({type: MyApp.Comment, content: []});
    this.set('comments', comments);
}
comments.pushObject(newComment);

しかし、これも機能しません。

だから私の質問は:ネストされたモデル構造を作成ember-resourceしてデータベースに保存するにはどうすればよいですか?

ヒントをありがとう!


アップデート:

ソースを閲覧した後ember-resource、問題を解決する1つの方法を見つけました。

var comments = this.get('comments');
if (!comments) {
    this.updateWithApiData({comments: []});
    comments = this.get('comments');
}
comments.pushObject(newComment);

このメソッドupdateWithApiDataは、RESTリソースからデータを読み取るときに使用されるメソッドのようです。

私はまだこれがそれを行うための最良の/正しい方法であるかどうか疑問に思います.....

4

0 に答える 0