Javascript 从模型列表加载的默认项

Javascript 从模型列表加载的默认项,javascript,model,ember.js,render,outlet,Javascript,Model,Ember.js,Render,Outlet,我从Ember.js开始,需要一些帮助 我有下面的两个把手。它们非常简单:只显示一个图像列表,如果用户单击其中一些图像,则在把手内的{outlet}上将其放大 这一切都很完美。但是当用户访问索引#/paints时,我需要显示一个默认图像,并且它需要是paints数组(模型装置)的第一项 当用户加载索引#/paints时,我真的无法发现如何自动打印把手内的把手绘画#绘画{{outlet}} 非常感谢 {{outlet}} {{{#各} {{{#链接到“绘画”这个} {{/链接到} {{

我从Ember.js开始,需要一些帮助


我有下面的两个把手。它们非常简单:只显示一个图像列表,如果用户单击其中一些图像,则在把手内的{outlet}上将其放大

这一切都很完美。但是当用户访问索引#/paints时,我需要显示一个默认图像,并且它需要是paints数组(模型装置)的第一项

当用户加载索引#/paints时,我真的无法发现如何自动打印把手内的把手绘画#绘画{{outlet}}

非常感谢



{{outlet}}
    {{{#各}
  • {{{#链接到“绘画”这个} {{/链接到}
  • {{/每个}}

只需设置集合中的第一个对象,您可以从路由器执行此操作,我假设您的代码中有一些内容,但您应该了解:

App.PaintingsRoute = Ember.Route.extend({
    /**
     * guessing model here is the collection of paintings
     */
    setupController: function(controller, model) {
        var paintingController = this.controllerFor('painting');
        paintingController.set('content', model.get('firstObject'));
        controller.set('content', model);
    }
});
你试过了吗?我想你可以这样做

App.Router.map(function() {
  this.resource('paintings');
  this.resource('painting', { path: '/painting/:painting_id' });
});

App.PaintingsRoute = Ember.Route.extend({
  afterModel: function(paintings, transition) {
    this.transitionTo('painting', paintings[0]);
  }
})
谢谢大家

更好的选择(我认为)是使用renderTemplate:

App.PrintsRoute = Ember.Route.extend({
  // ...
  renderTemplate: function(controller, model) {
    this.render('prints'); // the list of prints

    controller = this.controllerFor('print');
    controller.set('content', model.get('firstObject')); //loads the default print
    this.render('fullsize', {outlet: 'fullsize', controller: controller, into: 'prints'}) //shows it in the right place
  }
});

这里的问题是ember不会将绘画模板渲染到{{otulet}}automatically@edu建议对this.transition('painting',paints.get('firstObject')进行修改
。我从Ember.js网站获取了上述来源,但请告诉我答案是否应该编辑。之前(我进行编辑时)有
this.transitiono('post',posts[0])顺便说一句,绘画[0]的结果将与绘画相同。get('firstObject'),但最后一个是ember惯用的,谢谢您的澄清。当我再次查看答案时,我注意到我的复制/粘贴错误。非常感谢大家!几乎完成:)我正在使用嵌套路由,那么App.Router是:
App.Router.map(function(){this.resource('paints',function(){this.resource('painting',{path:':painting_id'});})
claptimes响应几乎完美,我只需要添加一个if
,因为我失去了直接的url访问(index.html#/paints/20):afterModel:function(paints,transition){if(transition.params.painting_name==null){this.transitiono('painting',paints.get('firstObject');}
,只有一个问题:如果用户再次单击绘画链接(下面的代码),afterModel不会启动,那么第一个图像不会再次加载。我正在搜索其他事件来解决这个问题:)
{{{{{链接到‘绘画’}}Pinturas{{{/链接到}
App.PrintsRoute = Ember.Route.extend({
  // ...
  renderTemplate: function(controller, model) {
    this.render('prints'); // the list of prints

    controller = this.controllerFor('print');
    controller.set('content', model.get('firstObject')); //loads the default print
    this.render('fullsize', {outlet: 'fullsize', controller: controller, into: 'prints'}) //shows it in the right place
  }
});