2013-05-28 2 views
3

종종 실제 모델 속성이 설정되지 않은 경우 내보기가 특정 값으로 기본 설정되기를 원합니다. 이 자리 표시 자 텍스트/값은 엄격하게보기 전용이므로 모델에 포함하면 안됩니다. 나는, '설명'과 '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에 대한 포인터가 실제로 도움이됩니다.

+0

내가 대신 defaultWith의 defaultTo 생각, https://github.com/emberjs/ember.js/blob/v1.0.0-rc.4/packages/ember-metal/lib/computed.js # L732 –

+0

@Unspecified 내 잘못입니다. 그것은 오타입니다. 내 질문 업데이트 중. – Rajat

+1

'defaultTo'가 더 이상 사용되지 않는다고 말하고 싶습니다 : https://github.com/emberjs/ember.js/pull/4979 – shane

답변

3

이것은 Ember.computed.defaultTo이 의도 한 것이 아닙니다. Ember.computed.defaultTo는 하나의 defaultPath 매개 변수를 사용합니다. 워드 프로세서 : defaultPath의 값을 표준 getter 및 setter하지만, 기본적으로 같은 역할을

계산 된 속성입니다.

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') 
}); 
관련 문제