2013-11-21 25 views
2

我知道EmberJS建議使用路線將模型,視圖和控制器連接在一起,這對於設置頁面的主要內容非常方便。不過,假設我有一個側邊欄來顯示第二個來源的信息。例如rss提要。或連接到某些服務器狀態的狀態面板, 我想要一個可添加到連接到單獨模型的任何模板{{view MessageView}}的視圖。當沒有路線存在時,鏈接視圖,控制器和模型的正確方式是什麼?

什麼是'燼途'來做到這一點。

到目前爲止,我能做的最好的事情是讓顯式的View和Controller對象覆蓋init函數以將它們連接在一起。 例如:

/* 
* 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}}。 (您也可以使用{{render}}的路線,如果您覆蓋路線的renderTemplate掛鉤無所事事)。

當你說{{render 'message'}},Ember將實例化MessageControllerMessageView並連接它們。

要連接到模型,您有兩個選擇:如果模型可以更改,或者有多個「消息」視圖,則可以傳遞模型以渲染:{{render 'message' someMessage}}。或者,如果你想確保在同一時間只顯示一個消息並處理路由加載它的地方,你可以這樣做:

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

setupController鉤知道someMessage應該是什麼樣的路線。這可能是ApplicationRoute。您也可以在應用程序初始化程序中執行此操作,但我認爲將其放入ApplicationRoute更靈活,更明確/可維護。

請注意,如果檢索someMessage的代碼是承諾,則需要將其設置在承諾的then中的控制器上。

相關問題