0

質問:

  1. このコード行は_activeAuthor.get('books').pushObject(book).save();Chrome でエラーなく処理されますが、本は Ember-Data の _activeAuthor インスタンスの books プロパティに追加されません。なぜだか分からない?
  2. 以下のコードは、作成された Book を Chapter インスタンスの Book プロパティに追加します (コメントを参照)。これは 1 対多の関係です (de モデルを参照)。Ember-Data は、Book インスタンスの関連レコードを自動的に入力するようです。これは Ember-Data の通常の動作ですか? Ember-Data に 1 対多の関係の関連する側を入力させるべきですか、それとも両側を指定して両方のインスタンスを保持するべきですか?
  3. 以下のコードの問題の 1 つは、I do not handle promises であると思われます。このコード:this.modelFor('user').get('latestChapter');約束を返すようです。でプロミスを処理するにはどうすればよいget()ですか?

コード:

createChapter: function() {
  //Getting the Author of the latestChapter or getting the first Author in the array
  var _activeAuthor = null;
  var authors = this.modelFor('user').get('authors').toArray();
  var latestChapter = this.modelFor('user').get('latestChapter');
  var latestAuthor = latestChapter.get('author');
  if (latestChapter.content) {
    _activeAuthor = latestAuthor;
  } else {
    _activeAuthor= authors[0];
  }

  var book = this.store.createRecord('book', {
    title: 'click here to name your book',
    author: _activeAuthor,
  });

  var chapter = this.store.createRecord('chapter', {
    title: 'Click here to name your chapter',
    book: book, // Add the created Book to the Book property of the Chapter instance
  });

  _activeAuthor.get('books').pushObject(book).save();
  chapter.save();
  book.save();
  this.modelFor('user').set('latestChapter', chapter).save() //Identifying the latest created chapter at the lastestChapter;
  console.log('New chapter created: ' + chapter.get('id'));
},

モデル:

App.Author = DS.Model.extend({
  type: DS.attr('string'),
  authorTitle: DS.attr('string'),
  userTitle: DS.attr('string'),
  description: DS.attr('string'),
  user: DS.belongsTo('user', {inverse: 'authors', async: true}),
  books: DS.hasMany('book', { inverse: 'author', async: true}),
});


App.Book = DS.Model.extend({
  title: DS.attr('string'),
  icon: DS.attr('string'),
  description: DS.attr('string'),
  frequency: DS.attr('string'),
  chapters: DS.hasMany('chapter', { inverse: 'book', async: true}),
  author: DS.belongsTo('author', { inverse: 'books', async: true}),
});


App.Chapter = DS.Model.extend({
  title: DS.attr('string'),
  description: DS.attr('string'),
  frequency: DS.attr('string'),
  unit: DS.attr('string'),
  aggregationMode: DS.attr('string'),
  dashboard: DS.attr('boolean'),
  statData : DS.attr('array'),
  book: DS.belongsTo('book', { inverse: 'chapters', async: true}),
});

ありがとう!

4

1 に答える 1

1

1. author.get('books')promise を返すので、おそらくやりたいことは

author.get('books').then(function(books) {
    books.pushObject(book)
});
author.save();

これが問題でなければ、アプリ コード全体で jsfiddle を提供していただけますか? そうすれば、助けやすくなります!:)

2.(サーバーと同期されていない)getモデルのプロパティがあるたびに、emberはサーバーに尋ね、はい、望ましい動作であるストアにレコードを入力します:)asyncisLoaded

3.asyncモデル プロパティがある場合、常に promise を取得するため、たとえば次のように処理する必要があります。

chapter.get('book').then(function(book) {
  // here's a book
});

ところでvar latestAuthor = latestChapter.get('author');->プロパティchapterがありませんauthor:)

于 2015-01-14T04:30:25.200 に答える