通常,如果未设置实际的模型属性,我希望我的视图默认为特定值。 此占位符文本/值仅严格查看,因此不应放在模型imo中。
所以,这就是我最终要做的事情:
// 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')
});
有关如何在定义所有属性上的默认值时减少样板的任何建议,例如'title','description'和'thumbUrl'?
我查看了 Ember.computed.defaultTo ,但未能理解我如何使用它。这就是我设想它的实际行动:
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')
});
那怎么办呢?
如果有更好的方法来做这种类型的事情,我想在评论中听到它们。
另外,指向 Ember.computed.defaultTo 的内容也会非常有用。
答案 0 :(得分:3)
这不是Ember.computed.defaultTo
的目的。 Ember.computed.defaultTo采用单个defaultPath
参数。来自文档:
计算属性,其作用类似于标准的getter和setter,但默认为
defaultPath
的值。
如果你阅读test,它就会有所启发。
testBoth('Ember.computed.defaultTo', function(get, set) {
var obj = { source: 'original source value' };
Ember.defineProperty(obj, 'copy', Ember.computed.defaultTo('source'));
equal(get(obj, 'copy'), 'original source value');
set(obj, 'copy', 'new copy value');
equal(get(obj, 'source'), 'original source value');
equal(get(obj, 'copy'), 'new copy value');
set(obj, 'source', 'new source value');
equal(get(obj, 'copy'), 'new copy value');
set(obj, 'copy', null);
equal(get(obj, 'copy'), 'new source value');
});
相反,您可以像这样编写自己的助手:
Ember.computed.defaultValue = function(dependentKey, defaultValue) {
return Ember.computed(dependentKey, function() {
return Ember.get(this, dependentKey) || defaultValue;
});
};
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.defaultValue('content.title', 'Title goes here'),
description: Ember.computed.defaultValue('content.description', 'This is your description'),
thumbUrl: Ember.computed.defaultValue('content.thumbUrl', 'http://placehold.it/100x100')
});
如果您不想创建自己的辅助函数,另一种方法是使用默认值的单独属性,然后使用Ember.computed.any
。
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>'),
defaultTitle: 'Title goes here',
title: Ember.computed.any('content.title', 'defaultTitle'),
defaultDescription: 'This is your description',
description: Ember.computed.any('content.description', 'defaultDescription'),
defaultThumbUrl: 'http://placehold.it/100x100',
thumbUrl: Ember.computed.any('content.thumbUrl', 'defaultThumbUrl')
});