简体   繁体   English

Ember.js:控制器何时可以访问模型?

[英]Ember.js: When is a model accessible to the controller?

In Ember, when is a model accessible to the controller? 在Ember中,控制器什么时候可以访问模型?

Below is a simple Ember application with a static model and a controller that attempts to access the model on init . 下面是一个简单的Ember应用程序,它具有静态模型和一个控制器,该控制器尝试在init上访问该模型。 You'll notice that on init the model title attribute is undefined . 您会注意到,在init ,模型title属性是undefined However, the setTimeout illustrates that the model is later accessible. 但是,setTimeout说明以后可以访问该model

I was under the impression that Ember wouldn't initiate the controller until the model is ready. 我的印象是,在模型准备就绪之前,Ember不会启动控制器。 Is this correct? 这个对吗? The Ember documentation ( http://emberjs.com/guides/routing/specifying-a-routes-model/ ) indicates that models, even when loaded asynchronously are available when the controller is instantiated. Ember文档( http://emberjs.com/guides/routing/specifying-a-routes-model/ )指出,即使实例化控制器时,即使异步加载,模型也可用。

http://jsfiddle.net/vu263uwq/1/ http://jsfiddle.net/vu263uwq/1/

App = Ember.Application.create({});

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return posts[0];
    }
});

App.IndexController = Ember.ObjectController.extend({
    alertTitle: function() {
        var that = this;

        jQuery(".output").append("1. "+this.get("title")+"<br />"); //Returns "undefined"

        setTimeout(function() {
            jQuery(".output").append("2. "+that.get("title")); //Returns the correct title
        }, 1000);
    }.on("init")
});

var posts = [
    {
        id: 1,
        title: "First title",
        body: "Cras mattis consectetur purus sit amet fermentum. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Nullam quis risus eget urna mollis ornare vel eu leo. Cras justo odio, dapibus ac facilisis in, egestas eget quam."
    },{
        id: 2,
        title: "Second title",
        body: "Maecenas faucibus mollis interdum. Vestibulum id ligula porta felis euismod semper. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum."
    }
];

Your impression is actually wrong. 您的印象实际上是错误的。 The setupController hook is where you set any properties on the controller, including the model. setupController挂钩是您在控制器上设置任何属性(包括模型)的位置。 Actually, all setupController does by default is set the model on the controller. 实际上,默认情况下,所有setupController所做的都是在控制器上设置模型。 That's why if you override the hook and still need the model set, you must explicitly set the model or make a call to this._super(controller, model) . 这就是为什么如果您重写该挂钩并仍然需要model集,则必须显式设置模型或调用this._super(controller, model)

Notice the signature for setupController: 注意setupController的签名:

setupController: function(controller, model)

In order for this to work, the controller must already be instantiated by the point where Ember sets the model. 为了使它起作用,必须已经在Ember设置模型的位置实例化了控制器。 Init is fired during the instantiation of the controller. 在控制器的实例化过程中会触发Init。 So a call to init happens before a model is set` 因此,在设置model之前会调用init

As for the asynchronous fetching of models, if your model hook returns a promise, Ember will block until a fulfilled promise can be passed into the setupController function so that the model is assigned to the promise's fulfillment and not the promise itself. 对于模型的异步获取,如果您的模型钩子返回了一个Promise,Ember将阻塞,直到可以将一个已实现的setupController传递到setupController函数中,以便将模型分配给Promise的Promise,而不是Promise本身。 It lets you avoid calls like: 它使您避免像这样的呼叫:

setupController: function(controller, model){  
   var self = this;
   makeAsyncCallReturningPromise().then(function(result){
       self._super(controller, result);
   }
}

which would basically accomplish the same thing 基本上可以完成同样的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM