【问题标题】:Emberjs 1.0.0-RC3: Is there a better way to set model property for #renderEmberjs 1.0.0-RC3:有没有更好的方法来为#render 设置模型属性
【发布时间】:2013-05-27 01:21:04
【问题描述】:

代码有效,这里是 jsfiddle for the code。但是,我不喜欢我用来让 {[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>

【问题讨论】:

    标签: ember.js handlebars.js ember-router


    【解决方案1】:

    我不确定你想做什么。您希望eventsappointments 在一页上同时呈现吗?如果是,那么,我认为,您需要使用 render (就像您在 jsfiddle 中所做的那样)。如果没有,您可以删除 ApplicationRoute 上的 {{render 'events'}}setupController 挂钩并重定向到 events 路由。

    我发现这个issueFixture Adapter 似乎不适用于hasMany(因此jsfiddle 示例不起作用)但我假设您使用的是RESTAdapter

    更新:

    另一个设置模型的地方是init钩子,像这样:

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

    就我个人而言,我会将代码留在setupController 挂钩中。

    【讨论】:

    • 谢谢。是的,我想在同一页面上呈现 eventsappointments。我读了链接。如果 returned JSON key 与模型中定义的相同,则 FixtureAdapter 应该加载 has_many。就我而言,它们是相同的,因此它应该可以工作,并且可以从控制台进行。但是当我使用控制器中的 needs api 时,我可以看到它在检查时返回 events 控制器内容属性 内的约会。但是当我尝试使用 get 来获取它失败的 apointments。
    • 为了使问题更加集中,我在这里将关于使用 needs apiFixtureAdapter 的第二个问题提取为一个独立的问题:@987654323 @
    猜你喜欢
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多