I am a ember.js novice and trying to rewrite the CRUD pages of a project in ember.js. But I meet a problem about Ember.Select.
Steps:
- On the create page, there is a DropDown control, whose content is fetched from the server side.
- After a user clicks submmit button, action "save" is invoked. I add a breakpoint in "save" action to watch distributiontarget.get('serviceDataModel').
- If I do not click the Dropdown box, the value of distributiontarget.get('serviceDataModel') is undefined. If I clicked the dropdown control and select one, the value of distributiontarget.get('serviceDataModel') is the value of url.
I think the reason is that the default value is not set for the Ember.Select after the data are returned from server. But I could not find a way to set the default vlaue. Ember Select set default value introduced a method. But this.store.find('distribution') returen a promise. How could we get the first object? In additon, how could I select an item in DropDown when I am going to work on the edit page?
APP.DistributiontargetsCreateController= Ember.ObjectController.extend({
distributions: function() {
return this.store.find('distribution');
}.property(),
actions: {
save: function(distributiontarget) {
..............
debugger;
}
}
});
APP.Distribution = DS.Model.extend({
name: attr(),
url: attr()
});
APP.Distributiontarget = DS.Model.extend({
organizationId : attr('string'),
name : attr('string'),
deliveryType : attr('string'),
host : attr('string'),
baseDir : attr('string'),
username : attr('string'),
password : attr('string'),
serviceDataModel : attr('string')
});
<form>
<div class="control-group required">
<label class="control-label">Username</label>
<div class="controls">
{{ view Ember.TextField valueBinding="username" }}
</div>
</div>
..................
<div class="control-group required">
<label class="control-label">Default Data Model</label>
<div class="controls">
<span>
{{view Ember.Select
content=distributions
optionValuePath="content.url"
optionLabelPath="content.name"
valueBinding="serviceDataModel"
}}
</span>
</div>
</div>
<div class="form-actions">
<button type="submit" {{action save this}}> Save Distribution</button>
</div>
</form>
The data returned from the server:
{
"distributions" : [ {
"name" : "Test One",
"url" : "http://192.168.204.164",
"id" : "1"
}, {
"name" : "Test Two",
"url" : "http://192.168.204.166",
"id" : "2"
}
}