vendredi 14 juillet 2017

Ember - Cannot createRecord with mutable array attribute

I am trying to make it so that every new collection record that is created, has a products attribute, whose value is an empty mutable array.

I got a model defined in /collection.js

import DS from 'ember-data';

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

and an action that creates a record of a collection locally with Ember-Data (in PouchDB). I am having trouble at the line where createRecord is called. After some console logs and creating collections myself, I realized that the saved collection records do not include a products attribute, just name and rev, it is like the line "products: []" gets ignored.

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

Instead of

products: []

and

products: Ember.A([])

of which, both seem like they dont get executed, I also tried

products: DS.MutableArray([])

which gives me

TypeError: DS.MutableArray is not a function

Then

products: DS.ManyArray

and

products: DS.MutableArray

that give me

"Assertion Failed: You must pass an array of records to set a hasMany relationship"

and

products: DS.ManyArray([])

that gives me

TypeError: Cannot call a class as a function

Any idea on how I can write that line is welcome! Thanks




Aucun commentaire:

Enregistrer un commentaire