2017-07-15 116 views
0

我正在試圖使它創建的每個新集合記錄都有一個產品屬性,其值是一個空的可變數組。不能創建具有可變數組屬性的記錄

我在/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的行遇到問題。在我自己創建了一個控制檯日誌並創建了一個集合之後,我意識到已保存的集合記錄不包含產品屬性,只是名稱和轉義,就像線條「products:[]」被忽略。

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); 
相關問題