1

in ember 2.7 with ember data i have a route with models created with 'RSVP.hash' like the example here :

model: function(params){
    return Ember.RSVP.hash({
        customer: this.store.findRecord('customer', params.customer_id),
        address: this.store.createRecord('address')
    });

I want to save the 'address' part of the model ? model.save() gives as result: 'Uncaught TypeError: model is not a function' what can i do to save only the address part of the model ?

this is is the action that saves the model :

 createAddress: function(model){
        console.log(model.address.constructor);
        model.address.save();

    }

models (address):

import DS from 'ember-data';

export default DS.Model.extend({
    id_address: DS.attr('number'),
    id_customer_fb: DS.attr(),
    id_customer: DS.attr('number'),
    id_supplier: DS.attr('number'),
    id_state: DS.attr('number'),
    id_country: DS.attr('number'),
    postcode:  DS.attr('string'),
    active: DS.attr('boolean'),
    address1: DS.attr('string'),
    address2: DS.attr('string'),
    city: DS.attr('string'),
    alias:  DS.attr('string'),
    company: DS.attr('string'),
    vat_number: DS.attr('string'),
    firstname: DS.attr('string'),
    lastname: DS.attr('string'),
    other: DS.attr('string'),
    phone: DS.attr('string'),
    phone_mobile: DS.attr('string'),
    deleted: DS.attr('boolean'),
//    date_add: DS.attr('date'),
//    date_upd: DS.attr('date'),
    customer: DS.belongsTo('customer')


});

models customer :

import DS from 'ember-data';

export default DS.Model.extend({
    id_count: DS.attr('number'),
    id_customer: DS.attr('number'),
    id_default_group: DS.attr('number'),
    id_lang: DS.attr('number'),
    id_gender: DS.attr('number'),
    active: DS.attr('boolean'),
    email: DS.attr(),
    firstname: DS.attr(),
    lastname: DS.attr(),
    company: DS.attr(),
    birthday: DS.attr('date'),
    date_add: DS.attr('date'),
    date_upd: DS.attr('date'),
    max_payment_days: DS.attr('number'),
    newsletter: DS.attr('boolean'),
    note: DS.attr(),
    website: DS.attr(),
    addresses: DS.hasMany('address')
});
Rudi Werner
  • 99
  • 1
  • 9
  • From the error messsage you give, the immediate problem is that you need to say `this.get('model').save()`. However, as another answered mentioned, you can't save a hash like that. SO you need `this.get('model.address').save()`. –  Aug 09 '16 at 16:37
  • this gives me : Uncaught TypeError: Cannot read property 'save' of undefined – Rudi Werner Aug 09 '16 at 16:50
  • You need to do some debugging. Examine the value of `this.get('model')`, then examine the value of `this.get('model.address')`. –  Aug 09 '16 at 17:04
  • See here under ... printouts of both ! Best regards – Rudi Werner Aug 09 '16 at 17:06

1 Answers1

0

The model that you're working with is nothing more than a hash. Ember.RSVP.hash({}) just returns a hash once all of the promises have resolved.

So in your case, if you'd like to save just the address, you could to model.address.save()

xcskier56
  • 611
  • 3
  • 11
  • `console.log(model)` & `console.log(model.address)`. The first should return an object like this: `{customer: {id: 1, ... }, address: {id: nil, ... }}` the second should return the unsaved instance of address. – xcskier56 Aug 09 '16 at 16:01
  • console.log(model) gives me Object {__ember_meta__: Meta}__ember_meta__: Metaaddress: (...)get address: GETTER_FUNCTION()arguments: (...)caller: (...)length: 0name: "GETTER_FUNCTION"prototype: Object__proto__: ()set address: SETTER_FUNCTION(value)customer: (...)get customer: GETTER_FUNCTION()set customer: SETTER_FUNCTION(value)__proto__: Object – Rudi Werner Aug 09 '16 at 16:16
  • and what does `console.log(model.address)` return? – xcskier56 Aug 09 '16 at 16:55
  • Class {store: Class, _internalModel: InternalModel, isError: false, adapterError: null, OWNER [id=__ember14707620881361177318154216]: Class…}OWNER [id=__ember14707620881361177318154216]: Class__ember1470762088136: "ember1678"__ember_meta__: Meta_internalModel: InternalModel_super: ROOT()adapterError: nullcontainer: (...)currentState: (...)get currentState: GETTER_FUNCTION()set currentState: SETTER_FUNCTION(value)id: (...)get id: GETTER_FUNCTION()set id: SETTER_FUNCTION(value)isError: falsestore: Class__proto__: Class – Rudi Werner Aug 09 '16 at 17:02
  • Ok, the output looks like an ember model. What does `console.log(model.address.constructor)` output? – xcskier56 Aug 09 '16 at 17:20
  • function cz-office@model:address: – Rudi Werner Aug 09 '16 at 17:25
  • Yes, that is an instance of an ember model and will respond to save. After the `console.log` statement try calling `model.address.save()` or depending on where you're calling it from, `this.get('model.address').save()`. As long as you're calling save on the same object you called `constructor` on it should save. – xcskier56 Aug 09 '16 at 17:34
  • model.address.save() gives me another error : Error: Some errors were encountered while saving cz-office@model:address: -KOkTRv0si5kJcvnADyA(…) I think his has to do with the fact i create a new record in the route and the fields are undefined ?? Possible ?? – Rudi Werner Aug 09 '16 at 18:06
  • Do you have any relation between address and customer model..pls post your model code in question – Ember Freak Aug 09 '16 at 18:13
  • This is a study project to learn ember and yess indeed i make (in earlier times) a relation one to many between Customer and address is this a problem ??? (See models above) – Rudi Werner Aug 09 '16 at 18:32
  • Like u guessed that will be the problem - refer http://emberjs.com/api/data/classes/DS.Store.html#method_createRecord i am not familiar with relations currently you have. i guess some one will show correct path ! – Ember Freak Aug 09 '16 at 18:56