emberjs transitionTo不渲染模型列表

时间:2014-02-20 19:07:46

标签: ember.js ember-data

更新:好的。我解决了这个问题。我认为这是因为我将新客户端模板渲染到ClientsNewRoute中的应用程序模板中(见下文)。我的理解(假设)是它不是在客户端模型的上下文中操作,因为它最初将新表单呈现到应用程序模板中,然后在尝试“transitionTo”客户端列表视图时将其视为父表单。

我有一个/ clients视图,列出了完成刷新后的所有客户端,但是从表单添加新客户端并使用transitionTo后,它不会呈现列表。日志显示transitionTo成功但我无法填充列表,除非我正在完全刷新客户端/路由。后端返回200,带有完整的模型对象。这可能是因为从子路径转换时未调用模型挂钩。有什么想法吗?

router.js

App.Router.map(function() {
  this.resource('clients', function(){
    this.route('new');
  });
  this.resource('client', { path: '/clients/:client_id' }, function(){
    this.route('edit');
  });

  this.resource('managers', function(){
    this.route('new');
  });
  this.resource('manager', { path: '/managers/:manager_id' }, function(){
    this.route('edit');
  });
  this.resource('projects', function(){
    this.route('new');
  });
  this.resource('project', { path: '/projects/:project_id' }, function(){
    this.route('edit');
  });
});

clients.route:

App.ClientsRoute = Ember.Route.extend({

  model: function(){
    return this.store.find('client');
  }

});

客户端/ new_route.js:

App.ClientsNewRoute = Ember.Route.extend({

renderTemplate: function() {
    this.render("clients/new", { 
        into: "application",
        outlet: "main"
})},

model: function(){
    return this.store.createRecord('client');
},
actions: {
    create: function(client){
        var route = this;
        client.save().then(function() {
            route.transitionTo('clients');
        }, function() {
            console.log('fail');
        });
    } 
  } 
});

由于

编辑:

的客户机/ new.handlebars:

<form {{action 'create' this on="submit"}} >

{{#if isSaving }}
saving record...
{{/if}}


<div class="row">

<div class="col-md-2"></div>
    <div class="col-md-8">
        <h2>New Client</h2>

        <table class="table table-striped">
            <tr> <td> Company Name </td> <td>{{input class="form-control" type="text" value=company_name}} </td> </tr>
            <tr> <td> First Name </td> <td> {{input class="form-control" type="text" value=first_name}} </td> </tr>
            <tr> <td> Last Name </td> <td> {{input class="form-control" type="text" value=last_name}} </td> </tr>
            <tr> <td> Email </td> <td> {{input class="form-control" type="text" value=email}} </td> </tr>
            <tr> <td> Phone </td> <td> {{input class="form-control" type="text" value=phone}} </td> </tr>
            <tr> <td> Description </td> <td> {{textarea class="form-control" value=description}} </td> </tr>
            <tr> <td> Address </td> <td> {{input class="form-control" type="text" value=street_address}} </td> </tr>
            <tr> <td> City </td> <td> {{input class="form-control" type="text" value=city}} </td> </tr>
            <tr> <td> State </td> <td> {{input class="form-control" type="text" value=state}} </td> </tr>
            <tr> <td> Zipcode </td> <td> {{input class="form-control" type="text" value=zip}} </td> </tr>

        </table>
        <button>Create</button>

    </form>
    </div>
  <div class="col-md-2"></div>
</div>

clients.handlebars:

<div class="row">
<div class="col-md-2"></div>
<div class="col-md-8">
    <h2>Clients</h2>

    <table class="table table-striped">
        <tr>
            <th>Company</th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
        {{#each}}
        <tr>
            <td>{{#link-to 'client' this}} {{company_name}}     {{/link-to}}</td>
            <td>    {{ first_name}} {{last_name}} </td>
            <td>  {{ email }} </td>
            <td>  {{phone}} </td>
        </tr>
        {{/each}}

    </table>

    <p> {{#link-to 'clients.new'}}Create a new Client{{/link-to}}</p>
</div> 
<div class="col-md-2">o</div>

记录信息:

Transitioned into 'clients.new' ember.js?body=1:3499
Transition #1: TRANSITION COMPLETE. ember.js?body=1:3499
Attempting transition to clients ember.js?body=1:3499
Transition #2: clients.index: calling beforeModel hook ember.js?body=1:3499
Transition #2: clients.index: calling deserialize hook ember.js?body=1:3499
Transition #2: clients.index: calling afterModel hook ember.js?body=1:3499
Transition #2: Resolved all models on destination route; finalizing transition. ember.js?body=1:3499
Could not find "clients.index" template or view. Nothing will be rendered Object {fullName: "template:clients.index"} ember.js?body=1:3499
Transitioned into 'clients.index' ember.js?body=1:3499
Transition #2: TRANSITION COMPLETE

直接请求路由时呈现的模板是clients.handlebars,位于templates /目录的根目录中。它没有插座,它包含显示客户表的#each循环。

2 个答案:

答案 0 :(得分:1)

我假设你的地图结构如下:

App.Router.map(function(){
    this.resource('clients', function(){
        this.route('new');
    });
});

您的问题是,从clients/new转换为clients时,模型未重新加载。这是对的吗?

如果是,请尝试创建另一条路线:

App.ClientsIndexRoute = Ember.Route.extend({
    model: function(){
        return this.store.find('client');
    }
});

答案 1 :(得分:0)

我解决了这个问题。我认为这是因为我将新客户端模板渲染到ClientsNewRoute中的应用程序模板中(见下文)。我的理解(假设)是它不是在客户端模型的上下文中操作,因为它最初将新表单呈现到应用程序模板中,然后在尝试“transitionTo”客户端列表视图时将其视为父表单。有关详细信息,请参见上文。