I have a model city
defined like this:
// app/models/city.js
import DS from 'ember-data';
var City = DS.Model.extend({
name: DS.attr('string'),
province: DS.belongsTo('province', {async: true})
});
As well as a model province
defined like this:
// app/models/province.js
import DS from 'ember-data';
var Province = DS.Model.extend({
name: DS.attr('string')
});
In my template I can view the cities and provinces like this:
{{!-- app/templates/cities/index.hbs --}}
<tbody>
{{#each city in controller}}
<tr>
<td>{{city.name}}</td>
<td>{{city.province.name}}</td>
</tr>
{{/each}}
</tbody>
From within my controller I would also like to access the province names.
// app/controllers/cities/index.js
import Ember from 'ember';
export default Ember.ArrayController.extend({
sortProperties: ['name'],
sortAscending: true,
...
someFunction: function() {
var cities = this.get('model');
cities.forEach(function(city) {
var name = city.get('name');
var province = 'unknown';
var promiseObject = city.get('province');
promiseObject.then(function() {
province = promiseObject.get('name');
console.log('Inside promise: province='+province);
});
console.log('Outside promise: province='+province);
});
}
});
Inside the promise the value of province
is set correctly, but when I log the value outside I see that it is still unknown
.
What am I doing wrong and is there a better more elegant way to access values via DS.belongsTo
?