簡體   English   中英

控制器,模型和路由器如何在ember中協同工作?

[英]how controller, model and router work together in ember?

我很困惑控制器,型號和路由器如何在余燼中一起工作? 是否有可能通過實例證明這一點?

這個模型在路線中定義的是什么,它與這個模型'app / models / someModel.js'的關系是什么?

應用程序/路由/ someRoute.js:

    export default Ember.Route.extend({
       model: function() {
         return something;
    }

謝謝

Ember致力於命名約定。 路由器將其模型函數返回的值設置為控制器的屬性model 因此,如果您在控制器中編寫this.get('model') ,您將看到路由器模型函數返回的值。

此外,如果您希望路徑的模型采用特定形式,則需要將其映射到已定義的對象。

App.SomeModel = Ember.Object.extend({
    firstName : '',
    lastName : '',

    fullName: function () {
       return this.get('firstName' + " " + this.get('lastName');
    }.property('firstName', 'lastName'),

    birthdate: function () {
       return moment(this.get('birth_date').format('MMMM Do YYYY');
    }.property('birth_date')
});

現在假設來自后端的數據采用以下格式:

{
  "firstName" : "PQR",
  "lastName" : "KLM",
  "birth_date" : "15 Oct 1992"
}

所以路由器將是

App.SomeRoute = Ember.Route.extend({
     model : function () {
         var promise = $.get('person.json');
         return promise.then(function (response) {
            return App.SomeModel.create(response); //Here response will be the JSON above.
         });
     }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM