Often, I want my views to default to a specific value if the actual model property is not set. This placeholder text/value is strictly view only and hence, should not be placed in the model imo.
So, this is what I end up doing:
// Sample 'Model' for illustration purposes only.
var myModel = Ember.Object.extend({
title: null,
description: null,
thumbUrl: null
});
/**
* Sample View
* Render view properties which are actually
* computed of the actual 'content' properties
*/
var myView = Ember.View.extend({
template: Ember.Handlebars.compile('<p>Title: {{view.title}}</p> <p>Description: {{view.description}}</p> <p>Image: <img {{bindAttr src="view.thumbUrl"}}/></p>'),
title: function () {
return this.get('content.title') || 'Title goes here';// placeholder 'title' text
}.property('content.title'),
description: function () {
return this.get('content.description') || 'This is your description'; // placeholder 'description'
}.property('content.description'),
thumbUrl: function () {
return this.get('content.thumbUrl') || 'http://placehold.it/100x100';
}.property('content.thumbUrl')
});
Any suggestions on how can I reduce boilerplate on defining defaults on all those properties i.e. 'title', 'description' and 'thumbUrl' ?
I looked into Ember.computed.defaultTo but failed to understand on how I can use it. This is how I envision it in action:
var myView = Ember.View.extend({
template: Ember.Handlebars.compile('<p>Title: {{view.title}}</p> <p>Description: {{view.description}}</p> <p>Image: <img {{bindAttr src="view.thumbUrl"}}/></p>'),
title: Ember.computed.defaultTo('content.title', 'Title goes here'),
description: Ember.computed.defaultTo('content.description', 'This is your description'),
thumbUrl: Ember.computed.defaultTo('content.thumbUrl', 'http://placehold.it/100x100')
});
So how can this be done ?
If there are better approaches to do this type of a thing, I would like to hear them in the comments.
Also, pointers to what Ember.computed.defaultTo does would be really helpful as well.
defaultTo
has been deprecated: github.com/emberjs/ember.js/pull/4979 – shane