dimanche 26 juillet 2015

How do I setup Ember-CLI belongsTo dropdown?

I am having issues saving a belongsTo relationship in Ember-CLI using Ember Data. I have a contact model that belongs to a client model. Here are the models:

// models/contact.js

import DS from 'ember-data';

var Contact = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    title: DS.attr('string'),
    email: DS.attr('string'),
    phone: DS.attr('string'),
    client: DS.belongsTo('client', {async: true}),
});


export default Contact;


// models/client.js
import DS from 'ember-data';

var Client = DS.Model.extend({
    name: DS.attr('string'),
    phone: DS.attr('string'),
    email: DS.attr('string'),
    summary: DS.attr('string'),
    contacts: DS.hasMany('contact', {async: true}), 
});

export default Client;

Here is my template:

<form {{action "save" on="submit"}}>

{{view "select" 
        contentBinding=model.client
        optionLabelPath="content.name"
        optionValuePath="content.id"
        selectionBinding="contact.client.content" }}
  <div>
    <label>
      First name
      {{input type="text" value=model.firstName}}
    </label>
  </div>
  <div>
    <label>
      Last name
      {{input type="text" value=model.lastName}}
    </label>
  </div>
  <div>
    <label>
      Title
      {{input type="text" value=model.title}}
    </label>
  </div>
  <div>
    <label>
      Email
      {{input type="text" value=model.email}}
    </label>
  </div>
  <div>
    <label>
      Phone
      {{input type="text" value=model.phone}}
    </label>
  </div>
  <div>
    <input type="submit" value="Save">
  </div>
</form>

This is the save mixin for this model:

import Ember from 'ember';

export default Ember.Mixin.create({
  actions: {
    save: function() {
      var route = this;
      var client = this.client;
      console.log(client)
      this.currentModel.save('contact', {client: client}).then(function() {
        route.transitionTo('contacts');
      }, function() {
        console.log('Failed to save the model');
      });
    }
  },
  deactivate: function() {
    if (this.currentModel.get('isNew')) {
      this.currentModel.deleteRecord();
    } else {
      this.currentModel.rollback();
    }
  }
});

I'm currently getting undefined from the console.log(client) and an Uncaught TypeError: this.currentModel.save is not a function from the line following.

As best I can tell, I'm having issues passing the selected client from the dropdown to the save mixin...

Any help/ideas would be much appreciated!




Aucun commentaire:

Enregistrer un commentaire