2012-12-24 1 views
1

여기서 간단한 예Ember에서 핸들 바에 의해 해석되는 동안 문자열을 출력하려면 어떻게해야합니까?

MyApp.ApplicationController = Ember.Controller.extend({ 
    content: "some @user", 
    parsedContent: function() { 
    return this.get("content").replace("@user", "<a {{action gotoUser 'user'}}>user</a>"); 
    }.property("content") 
}); 

야하는 대신 핸들 바 템플릿과 같은 내용을 해석하는 것이다 출력

some @user 
some <a {{action gotoUser 'user'}}>user</a> 

. 나는이 메뉴얼을 컴파일 할 필요가 있다고 생각하지만 올바른 컨텍스트를 전달하는 방법을 모르겠습니다.

Here's a JSFiddle

답변

2

나는 작업 예제 here 있습니다. 기본적으로 뷰 템플릿을 계산 된 속성으로 만들어서 템플릿을 수정 한 다음 Handlebars 파서에 전달할 수 있습니다.

MyApp.LinkedUsersView = Ember.View.extend({ 
    template: function() { 
     template = this.get("content").replace(/@(\w+)/g, '{{view MyApp.UserLinkView username="$1"}}'); 
     return Ember.Handlebars.compile(template); 
    }.property() 
}); 

MyApp.UserLinkView = Ember.View.extend({ 
    tagName: "span", 
    user: function() { 
     return MyApp.User.findByUsername(this.get('username')); 
    }.property(), 
    template: Ember.Handlebars.compile('<a {{action goToUser view.user href=true}}>@{{view.username}}</a>') 
}); 

{{view MyApp.LinkedUsersView content="some @user with @another_user"}} 

언제든지 Handlebars 템플릿을 컴파일하는 성능을 확신하지 못합니다.

+0

작동하는 것처럼 보이지만 데이터 바인딩이되지 않습니다. JSFiddle에서 MyApp.get ("router.applicationController"). set ("content", "whatever")'을 실행하면 템플릿을 다시 렌더링하지 않습니다. –

+0

다시 한번 말하지만, 성능 향상을 위해 핸들 바를 '{{#with content}} {{MyApp.LinkedUsersView contentBinding = "this"}} {{/ with}}' –

+0

으로 변경하십시오. 내용이 변경 될 때 뷰를 다시 렌더링하는 옵저버를 만듭니다. http://jsfiddle.net/bradleypriest/6p6XJ/253/ –