保存Ember-Data记录

时间:2015-01-14 03:49:13

标签: ember.js ember-data emberfire

问题:

  1. 此代码行_activeAuthor.get('books').pushObject(book).save();在Chrome中处理时没有错误,但该书未添加到Ember-Data的_activeAuthor实例的books属性中。我不明白为什么?
  2. 以下代码将创建的Book添加到Chapter实例的Book属性中(请参阅注释)。这是一对多的关系(参见de Models)。 Ember-Data似乎会自动填充Book实例上的相关记录。这是Ember-Data的正常行为吗?我应该让Ember-Data在一对多关系中填充关系的相关方面,还是应该指定双方并保留两个实例?
  3. 我怀疑以下代码的问题之一就是我没有正确处理承诺。此代码:this.modelFor('user').get('latestChapter');似乎返回了一个承诺。我该如何处理get()的投标?
  4. 代码:

    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}),
    });
    

    谢谢!

1 个答案:

答案 0 :(得分:1)

1。 author.get('books')将返回一个承诺,所以你可能要做的就是

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

如果这不是问题,你可以给一个jsfiddle整个应用程序代码吗?然后,它会更容易帮助! :)

2。 每次get模型的属性为async而不是isLoaded(未与服务器同步)时,ember会询问服务器,是的,将填写记录中的你的商店是一种理想的行为:)

3。 如果你有一个async模型属性,那么你总是得到一个承诺,所以你应该以这种方式处理它:

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

BTW var latestAuthor = latestChapter.get('author'); - > chapter没有author属性:)