2017-07-15 95 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); 
相关问题