簡體   English   中英

Ember.js Route.setupController與ObjectController

[英]Ember.js Route.setupController with ObjectController

我不知道為什么RouteController如何影響默認Model 這是一個例子。

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model); //returns undefined
        controller.set("title", "Page title");
    }
});

上面的代碼片段可以正常工作 模板將按預期打印{{title}} 注意該模型是“未定義的”。

App.ApplicationRoute = Ember.Route.extend({
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model); //returns undefined
        controller.set("title", "Page title");
    }
});

App.ApplicationController = Ember.ObjectController.extend({});

上面的代碼拋出錯誤 ...

(處理路由時發生錯誤:索引斷言失敗:無法將set('title',Page title)委派給對象代理的'content'屬性:其'content'未定義。)

...並產生空白頁。 解決方案是返回模型(空白對象)或使用Controller (默認行為)代替ObjectController 有人可以解釋這種特殊情況嗎? 為什么Ember在使用ObjectController時不假定為空對象? 是否假設對象將在商店或服務器中傳遞或從中檢索?

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return {};
    },
    setupController: function(controller, model) {
        this._super(controller, model);
        console.log(model);
        controller.set("title", "Page title");
    }
});

App.ApplicationController = Ember.ObjectController.extend({});

Ember文檔所述

Ember.ObjectController是Ember的Controller層的一部分。 它旨在包裝單個對象,代理獲取和設置到基礎模型對象的未處理嘗試,並將未處理的操作嘗試轉發到其目標。

ObjectController希望存在一個模型並將其設置為內容。 它基本上是單個對象的包裝。

暫無
暫無

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

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