0

代码有效,这里是代码的jsfiddle。但是,我不喜欢我用来让{[render}}工作的方法。现在为了能够在应用程序模板中使用{{render events}},我需要使用 setupController 在应用程序路由中设置它,然后调用controllerFor('events'),否则它将不起作用。但是我已经定义了一个带有模型挂钩的 EventsRou​​te,用于设置控制器内容,并且更喜欢{{render helper}}使用在 EventsRou​​te 中设置的模型。

我想知道除了我目前去 ApplicationRoute 的方法之外,是否有更好或更惯用的方法在 ember 中执行此操作

相关代码*

  App.Router.map(function(){
    this.resource('events');
  });

应用程序模板中的 {{render 'events'}} 仅在我通过应用程序路由设置模型时才有效。

 App.ApplicationRoute = Ember.Route.extend({
   setupController: function(){
     this.controllerFor('events').set('model', App.Event.find());        
   }
 });

我更喜欢 {{render 'events'}} 来处理此处设置的内容,但事实并非如此。但我现在保留在使用 {{#linkTo}} 可能有意义的地方使用。

 App.EventsRoute = Ember.Route.extend({
    model: function(){
    return App.Event.find();  
    },

   setupController: function(controller, model){ 
         controller.set('content', model);
    }
});

模板

   <script type="text/x-handlebars" data-template-name="application">
     {{render 'events'}}
      {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="events">

        <button {{action 'yEmpty'}}> log event content is empty</button>

       {{#each controller}}
        <h3>{{title}}</h3>
       <p>{{start}} - {{end}}</p> 
      {{/each}}
    {{outlet}}
 </script>
4

1 回答 1

0

I'm unsure of what you're trying to do. Do you want events and appointments to be rendered at the same time on one page? If yes, then, I think, you need to use render (much like you do in your jsfiddle). If not, you could remove {{render 'events'}} and setupController hook on ApplicationRoute and redirect to events route.

I found this issue with Fixture Adapter and looks like it doesn't work with hasMany (therefore jsfiddle example won't work) but I assume you are using RESTAdapter.

UPDATE:

Another place to set the model is in init hook like so:

App.EventsController = Ember.ArrayController.extend({
  init: function () {
    this.set('model', App.Event.find());
  }
});

Personally, I would left the code in setupController hook.

于 2013-05-27T09:12:25.007 回答