종종 실제 모델 속성이 설정되지 않은 경우 내보기가 특정 값으로 기본 설정되기를 원합니다. 이 자리 표시 자 텍스트/값은 엄격하게보기 전용이므로 모델에 포함하면 안됩니다. 나는, '설명'과 'thumbUrl' '제목'즉 모든 속성에 기본값을 정의하는 상용구를 줄일 수있는 방법에Emberjs DefaultTo 계산 된 속성
// 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')
});
어떤 제안 :
그래서,이 내가하고 결국 무엇인가?
나는 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에 대한 포인터가 실제로 도움이됩니다.
내가 대신 defaultWith의 defaultTo 생각, https://github.com/emberjs/ember.js/blob/v1.0.0-rc.4/packages/ember-metal/lib/computed.js # L732 –
@Unspecified 내 잘못입니다. 그것은 오타입니다. 내 질문 업데이트 중. – Rajat
'defaultTo'가 더 이상 사용되지 않는다고 말하고 싶습니다 : https://github.com/emberjs/ember.js/pull/4979 – shane