1
votes

I'm trying to create an empty record along with empty associated records.

Here's a fiddle: http://jsfiddle.net/brancusi/NQKvy/629/

    App = Ember.Application.create({});

    App.IndexRoute = Ember.Route.extend({
      model: function(){
          return this.createEmptyInventory();
      },

      createEmptyInventory: function(){

          var stockLevels = []

          var product1 = this.store.createRecord('product', {name:"Cheese Pizza"});
          var product2 = this.store.createRecord('product', {name:"Mushroom Pizza"});
          var product3 = this.store.createRecord('product', {name:"Calzone"});

          var products = [product1, product2, product3]

          var stockLevelList = []

          scope = this;

          Ember.EnumerableUtils.forEach(products, function(item){
              var stockLevel = scope.store.createRecord('product_stock_level', {
                  product:item,
                  quantity:0
              });

              stockLevelList.addObject(stockLevel);
          });

          var inventory = this.store.createRecord('inventory', {
              testProp:"TestVal",
              productStockItems:stockLevelList
          })

          console.log(stockLevelList);
          console.log(inventory.get('productStockLevels'));

          return inventory;
        }

    });

    // Models

    App.Inventory = DS.Model.extend({
        productStockLevels:DS.hasMany('product_stock_level'),
        testprop:DS.attr('string')
    });

    App.ProductStockLevel = DS.Model.extend({
        inventory:DS.belongsTo('inventory'),
        product:DS.belongsTo('product'),
        quantity:5
    });

    App.Product = DS.Model.extend({
        productStockLevels:DS.hasMany('product_stock_level'),
        name:DS.attr('string')
    });

So I notice that there is no ID, and I'm guessing that's how ember does all its associations?

How should I generate a temporary record like this?

If the user abandons this, I don't want to persist anything to the DB, I just want to then dump that record.

I'm basically trying to simulate a Rails: Inventory.new

1

1 Answers

0
votes

The problem is not that products do not have an id but with your array of records.

I am not sure if it is possible to create an array of records first and then use it as a property on createRecord. Never done this and not so in with Ember yet. But if it is possible I think it could not be a normal array but must be a DS.ManyArray or DS.RecordArray or something like this.

I am using another approach: I create the parent record without children first and then push the children to the parent using pushObject. So I would change the code for createEmptyInventory like this:

   createEmptyInventory: function(){    
      var stockLevels = []

      var product1 = this.store.createRecord('product', {name:"Cheese Pizza"});
      var product2 = this.store.createRecord('product', {name:"Mushroom Pizza"});
      var product3 = this.store.createRecord('product', {name:"Calzone"});

      var products = [product1, product2, product3]

      scope = this;

      var inventory = this.store.createRecord('inventory', {
          testProp:"TestVal"
      })

      Ember.EnumerableUtils.forEach(products, function(item){
          var stockLevel = scope.store.createRecord('product_stock_level', {
              product:item,
              quantity:0
          });

          inventory.get('productStockLevels').pushObject(stockLevel);
      });

      return inventory;
    }

I forked and updated your JSFiddle to show it working. Also fixed a little error in your view: http://jsfiddle.net/4YAVq/