2017-07-15 9 views
0

作成されたすべての新しいコレクションレコードに値が空の可変配列であるproducts属性があるようにしようとしています。変更可能な配列属性を持つレコードを作成できません

私は/collection.js

import DS from 'ember-data'; 

export default DS.Model.extend({ 
     name: DS.attr('string'), 
     products: DS.hasMany('product'), 
     rev: DS.attr('string') 
}); 

と/view-collections.jsルートで定義されたモデルを得ま​​した。このルートでは、createCollectionはローカルコレクションのレコードを作成するアクションです(Ember Data機能を使用するPouchDBを使用しています)。私は、createRecordが呼び出される行で問題を抱えています。いくつかのコンソールログを作成して自分でコレクションを作成した後、保存されたコレクションレコードには製品属性が含まれていないことがわかりました。

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model() { 
    return this.store.findAll('collection'); 
    }, 

    titleToken: 'Collections', 

    actions: { 
    createCollection() { 
     let route = this, 
      controller = this.get('controller'); 

     let collection = this.store.createRecord('collection', { 
     name: controller.get('newName'), 
     products: [] 
     }); 
     return collection.save().then(function() { 
     controller.set('newName', ''); 
     //route.transitionTo('products.product.collections', product); 
     }); 
    } 
    } 
}); 

代わりの

products: [] 

products: Ember.A([]) 

、彼らが実行されますいけないように、両方のは、私はまた、次の

products: DS.MutableArray([]) 
products: DS.ManyArray 
products: DS.MutableArray 
products: DS.ManyArray([]) 

の全てを試してみました、ように見えます私にエラーを与えてください。最後に、私の見解-collections.hbsに

{{#link-to 'products' class="button"}} 
    Back to products 
    {{/link-to}} 
     {{input type="text" class="new-collection" placeholder="New Collection" value=newName insert-newline="createCollection" }} 
     <button class="btn btn-primary btn-sm new-collection-button" {{action "createCollection"}} 
     disabled={{disabled}}>Add</button> 

{{#each model as |collection|}} 
     <div>{{collection.name}} {{collection.products}} {{collection.id}} 
     </div> 
    {{/each}} 

私はすべてのコレクションのすべての情報を印刷する場合は、それが実際には{{}} collection.productsのオブジェクトを検索し、すべてのコレクションのために、以下を出力します。

<DS.PromiseManyArray:ember498> 

この最後の部分はどのようなものか、どのように書くことができますか?「製品:[]」と書かれています。ありがとう

答えて

0

productの値を指定しないと、コレクションのためにcreateRecordできます。これはうまくいくでしょう。
レコードを作成するときに製品レコードを含め、それを指定しない場合は、pushObjectを使用できます。

let collection = this.store.createRecord('collection', { 
     name: controller.get('newName') 
     }); 
//createRecord for product 
let product = this.store.createRecord('product'); 
collection.get('products').pushObject(product); 
関連する問題