2013-11-21 5 views
2

EmberJS는 Routes를 사용하여 모델, 뷰 및 컨트롤러를 연결하는 것을 권장하며 페이지의 기본 콘텐츠를 설정하는 데 매우 편리합니다. 그러나 두 번째 소스의 정보를 표시하는 사이드 바가 있다고 가정합니다. 예 : RSS 피드. 또는 일부 서버 통계에 연결된 상태 패널 별도의 모델에 연결하는 모든 템플릿 {{View MessageView}}에 추가 할 수있는보기가 필요합니다.경로가없는 경우보기, 컨트롤러 및 모델을 연결하는 올바른 관용구는 무엇입니까?

이것을 수행하는 '엠버 웨이'는 무엇입니까?

지금까지 내가 할 수있는 최선의 방법은 명시 적 View 및 Controller 객체를 초기화 함수로 재정 의하여 연결하는 것입니다. 예를 들어 :

/* 
* The messages class is a free floating list of messages that can be plugged into any page. 
* it is implemented as a view with a separate controller and model. 
* As its not associated with a route we need to bind the view, controller and model together directly using 
* init statements. 
*/ 

/************************** 
* Model 
**************************/ 

App.Message = DS.Model.extend({ 
    msg: DS.attr('string'), 
    author: DS.attr('string'), 
    date: DS.attr('date'), 
    display_class: DS.attr('string') 
}); 


/************************** 
* Route 
**************************/ 

//App.MessageRoute = Ember.Route.extend({ 
// model : function() { 
//  return this.get('store').findAll('message'); 
//  } 
//}); 


/************************** 
* Views 
**************************/ 
App.MessageView = Em.View.extend({ 
     init: function() { 
//   this._super(); 
      this.set("controller", App.MessageController.create()); 
      }, 

    }); 


/************************** 
* Controllers 
**************************/ 
App.MessageController = Em.ArrayController.extend({ 
     init: function() { 
//   this._super(); 
      this.set("model", model : function() { 
       return this.get('store').findAll('message'); 
      }); 
      }, 

}); 


/************************** 
* Fixture 
**************************/ 
App.Message.FIXTURES = [{ 
     id: 1, 
     msg: "App website will be down for maintenance for 4 hours on Friday 29th Nov", 
     author: { name: "Admin" }, 
     date: new Date('2013-11-18T19:00:00Z'), 
     display_class: 'warning' 
    }, { 
     id: 2, 
     msg: "Tropical Cyclone may affect North Island net week [read more](http://www.bom.gov.au/cyclone/)", 
     author: { name: "WeatherMan" }, 
     date: new Date('2013-11-18'), 
    }, 
    ]; 

그러나 정말 필요한 것은 경로 기능이 이미 무엇을 재현 할 수있는 방법입니다.

더 좋은 방법이 있습니까?

여러 개의 독립된보기 영역이있는 대시 보드 스타일 페이지의 예가 있습니까?

감사합니다 앤드류

답변

2

함께 모델을 묶을 수있는 엠버 방법, 경로없이 컨트롤러 &보기 {{render}} 또는 {{control}}이다. (경로의 renderTemplate 후크를 무시하고 아무 것도하지 않으면 {{render}}과 함께 사용할 수도 있습니다.

{{render 'message'}}라고 말하면 Ember는 MessageControllerMessageView을 인스턴스화하고 연결합니다.

모델에 연결하려면 두 가지가 있습니다. 모델이 변경되거나 여러 개의 '메시지'보기가있는 경우 모델을 전달하여 {{render 'message' someMessage}}을 렌더링 할 수 있습니다. 당신은 단지 하나의 메시지가 한 번에 표시되어 있는지 확인하고 경로에 넣기 처리하는 경우 또는 어딘가에 당신은 할 수 있습니다 :

this.controllerFor('message').set('model', someMessage); 

을 someMessage이 있어야 할 것을 알고 경로의 setupController 후크. 이것은 ApplicationRoute 일 수 있습니다. 응용 프로그램 초기화 프로그램에서이 작업을 수행 할 수도 있지만, ApplicationRoute에 넣는 것이 더 유연하고 명확한/유지 관리가 가능하다고 생각합니다.

someMessage를 검색하는 코드가 약속 인 경우 약속의 then 컨트롤러에 설정해야합니다.

관련 문제