简体   繁体   English

将模型与emberjs中的路线相关联

[英]associating a model with a route in emberjs

As I understad, a template in emberjs gets it's data from controller. 据我所知,emberjs中的模板从控制器获取数据。 So, it's a controller's job to get hold of model data and present it to the template. 因此,掌握model数据并将其呈现给模板是控制器的工作。

The docs here associate a model with a route like this: 此处的文档将模型与以下路线相关联:

App.FavoritesRoute = Ember.Route.extend({
  model: function() {
    // the model is an Array of all of the posts
    return App.Post.find();
  }
});

In this case and ArrayController is automatically generated. 在这种情况下,将自动生成ArrayController


However, there's also the setupController function. 但是,还有setupController函数。 So, can we also do this : 因此,我们也可以这样做:

App.FavoritesRoute = Ember.Route.extend({
  setupController: function(controller) {
    controller.set('model', App.Post.find());
  }
});

as the first example given here do? 作为这里给出的第一个例子呢?

Do the two ways do the same thing? 两种方式做同一件事吗?

Do the two ways do the same thing? 两种方式做同一件事吗?

Almost. 几乎。 In both cases the controller's content property will be set to the result of App.Post.find() . 在这两种情况下,控制器的content属性都将设置为App.Post.find()的结果。 And both will work . 两者都会起作用

That said, using the model hook is the preferred way to do this. 也就是说,使用模型挂钩是实现此目的的首选方法。 If your model hook returns a promise, the router will wait for it to resolve before moving on. 如果您的模型挂钩返回了承诺,则路由器将等待其解决后再继续。 That is not the case with the setupController hook. setupController挂钩不是这种情况。 generally you will want to avoid anything async from the setupController hook. 通常,您将希望避免setupController钩子出现任何异步现象。

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

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