Your model is an array from looking at that screenshot (content: Array[5]
), so you need to get the first object if you want a single item:
export default Ember.Controller.extend({
url: Ember.computed('model', function() {
var logs = this.get('model');
var firstLog = logs.get('firstObject');
var path = firstLog.get('visited_resource');
var basePath = 'http://example.com';
return basePath + path;
});
});
or iterate over each item and return an array:
export default Ember.Controller.extend({
urls: Ember.computed('model', function() {
var logs = this.get('model');
var basePath = 'http://example.com';
var urls = logs.map(function(model) {
var path = model.get('visited_resource');
return basePath + path;
});
return urls;
})
});
To create a computed property on the model, then do it on the model:
export default DS.Model.extend({
ip: DS.attr('string'),
timestamp: DS.attr('number'),
visited_resource: DS.attr('string'),
pretty_string: DS.attr('string'),
url: Ember.computed('visited_resource', function() {
var basePath = 'http://example.com';
var path = this.get('visited_resource');
return basePath + path;
})
});
and you'll be to access the url
property like any other property:
export default Ember.Controller.extend({
url: Ember.computed('model', function() {
var logs = this.get('model');
var firstLog = logs.get('firstObject');
return firstLog.get('url');
})
})
You can also explicitly set the model using setupController
:
export default Ember.Route.extend({
controllerName: 'logs',
model: function() {
return this.store.find('logs');
},
setupController: function(controller, model) {
controller.set('model', model);
// or first item only
controller.set('model', model.get('firstObject'));
}
});
Best Solution
I've figured it out eventually.
plan = this.get('model')
works for the action. It returns the model, and the properties can be accessed withplan.get('price')
. Not ideal, but it gets the job done. Why it didn't work is because it was inside a function that was called as a callback from inside the action. So probably the scope of "this" wasn't carried out to the callback function as well. I moved the callback function as an inner function inside the action, then "this" scope worked.As for the scope problem, here's the solution setting an application controller variable to results returned from AJAX call