2
votes

using ember.js 1.0 and ember-data 1.0 beta2

I have a model (state) with the following properties

state: DS.attr('string'),
stateName: DS.attr('string'),

and a model (customer) with the following properties

name: DS.attr('string'),
stateID: DS.attr('string'),
state: DS.belongsTo("state")

I want to be able to edit the customer and choose the state from a drop-down (that has the stateID + name showing : eg "FL - Florida" and when selected, to store the state.stateID into the customer.stateID property

this is the first time I've tried something like this , and am slightly confused about the process.

In my customer route I've set up the following:

setupController: function(controller, model) {
    this._super(controller, model);
    this.controllerFor('state').set('content', this.store.find('state'));
}

and my select is this:

{{view Ember.Select
   contentBinding="controllers.state.content"
   optionValuePath="content.stateName"
   optionLabelPath="content.stateName" 
   valueBinding="content.stateID"
   selectionBinding="content.stateID"
   prompt="Select a state"
}}

now I'm confused about where to go from here.

thanks

update

changed the view to say

{{view Ember.Select
   contentBinding="controllers.state.content"
   optionValuePath="content.stateID"
   optionLabelPath="content.stateName"
   valueBinding="customer.stateID"
}}

and I still don't get the stateid property to change . I've also tried

selectionBinding="customer"

to no avail.

update #2

I suspect that my problem may be linked to the property name. I changed the customer.stateID property to be customer.foobar and changed the select to read

{{view Ember.Select
  contentBinding="controllers.state.content"
  optionValuePath="content.stateName"
  optionLabelPath="content.stateName"
  valueBinding="foobar"
  class="form-control"

}}

and now customer.foobar is updated with the value from the select.

Is there a problem with a property called stateID on customer ? I have a state model and state controller etc so is there a conflict ?

2
Can you post a jsbin or fiddle explaining your problem ? - Hyder
I am really struggling to get a small working sample. JS newbie here :( - jmls

2 Answers

1
votes

after all that - the problem was in the models themselves. The state model does not have a stateID field, it's state.state ...

My heartfelt apologies to all that wasted their time on this. Such a stupid error.

0
votes

Okay, maybe not the best solution, but it works well:

App.ItemModalController = Ember.ObjectController.extend({
  content: [],

  availableCategories: function() {
    return this.store.find('category');
  }.property(),

  //...
});

And the select:

{{view Ember.Select
  contentBinding="availableCategories"
  valueBinding="categorySelected"
  optionLabelPath="content.title"
  optionValuePath="content.id"
  prompt="Please select a category"
  class="form-control"
}}