2014-02-20 43 views
2

更新:好的。我解決了這個問題..我認爲這是因爲我正在將新客戶端模板渲染到ClientsNewRoute的應用程序模板中(請參見下文)。我的理解(假設)是,它不在客戶端模型的上下文中操作,因爲它最初將新窗體呈現到應用程序模板中,然後在嘗試「transitionTo」客戶端列表視圖時將其視爲父窗體。emberjs transitionTo不呈現模型列表

我有一個客戶端視圖,列出所有的客戶端,當做一個完整的刷新,但從窗體中添加一個新的客戶端,並使用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並位於根目錄o f模板/目錄。它沒有插座,它包含顯示客戶端表的#each循環。

+1

你可以張貼把手嗎?控制器是否有重要的意義 – claptimes

+1

您需要提供「ClientsRou​​te」的信息,因爲那是有問題的區域。此外,這些定義爲地圖上的「路線」或「資源」? – gravityplanx

+0

爲客戶端和客戶/新增路線以及車把模板添加路線。這裏唯一的出口是在我的application.handlebars文件中。 – errata

回答

0

我解決了這個..我想這是因爲我是呈現新的客戶端模板到ClientsNewRoute應用程序模板(見下文)。我的理解(假設)是,它不在客戶端模型的上下文中操作,因爲它最初將新窗體呈現到應用程序模板中,然後在嘗試「transitionTo」客戶端列表視圖時將其視爲父窗體。詳情見上文。

1

我假設你有你的地圖結構是這樣的:

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

而且您的問題是,從過渡到clients/newclients,模型沒有被重新加載。它是否正確?

如果是這樣,嘗試創建另一條路線:

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

這是正確的。我還在上面添加了我的route.js文件。我會嘗試的。 – errata

+0

我還添加了一些日誌信息。 – errata

+1

是的,這絕對看起來像你只需要添加索引路線!你試過了嗎? – gravityplanx