2013-04-02 242 views
0

試圖遵循this示例(現在已將更新的ember更新爲1.0rc2),並且我想嵌套模板以開始使用。我在home模板中有第一個額外的{{outlet}},我希望它包含兩個嵌套視圖(一個「簡歷」span9和另一個「工具表」span3爲您的引導粉絲)。 「簡歷」將再次嵌套模板,但是一旦我能夠通過這個第一級別,我想我可以做到其他。在Ember.js中嵌套模板

根據this SO?,和我的編輯JS Fiddle answer,我有點看它是怎麼做的,然而,答案是在CoffeeScript中,並且在本例中,路由器是一個比較複雜一點比我很能理解(使用connectOutlets ,考慮到它的一些輔助方法,這是我自己種理解。

最終的結果應該是這個樣子this fiddle

但我怎麼會得到其他模板在家用路由器渲染,作爲sections路由器和items路由器處於不同的路徑,但是,這些將成爲索引路線的一部分/?我是否需要多個{{outlets}}?以及如何將適當的模板路由到合適的插座佔位符?

信息:

DEBUG: ------------------------------- 
DEBUG: Ember.VERSION : 1.0.0-rc.2 
DEBUG: Handlebars.VERSION : 1.0.0-rc.3 
DEBUG: jQuery.VERSION : 1.9.1 
DEBUG: ------------------------------- 

的index.html:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>Resume Builder</title> 
    <meta name="description" content="A way to create a tailored resume from my CV" /> 
    <meta name="author" content="Chris Frisina" /> 

    <!--[if lt IE 9]> 
     <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 

    <link href="css/bootstrap.min.css" rel="stylesheet" /> 
    <style> 
     body { 
     padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ 
     } 
    </style> 
    <link href="css/bootstrap-responsive.css" rel="stylesheet" /> 
    <link href="css/RB.css" rel="stylesheet" /> 
    </head> 

    <body> 

    <script src="js/libs/jquery-1.7.1.js"></script> 
    <script src="js/libs/jquery.lorem.js"></script> 
    <script src="js/libs/bootstrap.min.js"></script> 
    <script src="js/libs/handlebars-1.0.0.beta.6.js"></script> 
    <!-- // <script src="js/libs/ember1.js"></script> --> 
    <script src="js/libs/ember2.js"></script> 
    <script src="js/RB.js"></script> 

    <script type="text/x-handlebars" data-template-name="application"> 
     <div class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="navbar-inner"> 
      <div class="container-fluid"> 
      <div class="brand home" {{bindAttr class="isHome:active"}} {{action "doHome"}}>Chris Frisina</div> 
      <div class="nav-collapse pull-right"> 
       <ul class="nav"> 
       <li class="sections"><a>1</a></li> 
       <li class="sections"><a>2</a></li> 
       </ul> 
      </div> 
      </div> 
     </div> 
     </div> 

     <div class="container-fluid"> 
     {{outlet}} 
     </div> 
    </script> 

    <script type="text/x-handlebars" data-template-name="index"> 
     <h3 class="demo-panel-title">This is the index template</h3> 
     {{home}} 
    </script> 

    <script type="text/x-handlebars" data-template-name="home"> 
     <div class="row-fluid"> 
     <!-- This is where I resume template should be placed --> 
     {{resume}} 
     <!-- This is where I tooltable template should be placed --> 
     {{tooltable}} 
     </div> 
    </script> 

    <script type="text/x-handlebars" data-template-name="resume"> 
     <div class="span9"> 
     <h1>Viewer</h1> 
     </div> 
    </script> 

    <script type="text/x-handlebars" data-template-name="tooltable"> 
     <div class="span3"> 
     <div id="tooltable" class="tooltable hero-unit affix"> 
      <h1>Tooltable</h1> 
      <p>Selector 1</p> 
      <p>Selector 1</p> 
     </div> 
     </div> 
    </script> 

    </body> 
</html> 

app.js:

// Attach the application to window 
window.RB = Ember.Application.create({}); 

// Define the main application controller. This is automatically picked up by 
// the application and initialized. 
RB.ApplicationController = Ember.Controller.extend({ 

}); 
RB.ApplicationView = Ember.View.extend({ 
    templateName: 'application' 
}); 

// Router 
RB.Router = Ember.Router.extend({ 

}); 
RB.Router.map(function(){ 

}); 

// Home 
RB.HomeController = Ember.Controller.extend({ 
}); 
RB.HomeView = Ember.View.extend({ 
    templateName: 'home', 
}); 
// Resume 
RB.ResumeController = Ember.ObjectController.extend({ 
}); 
RB.ResumeView = Ember.View.extend({ 
    templateName: 'resume' 
}); 

// Tooltable 
RB.TooltableController = Ember.ObjectController.extend({ 
}); 
RB.TooltableView = Ember.View.extend({ 
    templateName: 'tooltable' 
}); 

//Defer the start until advanceReadiness() is complete 
// cant init RB manually, must use advanceReadiness(), which needs defer... 
RB.deferReadiness(); 
// var test = function() { 
    RB.advanceReadiness(); 
// }); 

回答

3

我不能回答整個問題,但我可以通過給開出一個如何嵌套你的模板並寫出一個工作路線的例子

App.Router.map(function(match) { 
    this.resource("days", { path: "/days" }, function() { 
     this.resource("day", { path: "/:day_id" }, function() { 
      this.resource("appointments", { path: "/appointments" }, function() { 
       this.resource("appointment", { path: "/:appointment_id" }, function() { 
        this.route("edit", { path: "/edit" }); 
       }); 
      }); 
     }); 
    }); 
}); 

對於例如路由上面我打算讓用戶點擊從/天到某一天/天/ 1

那麼一天,我希望他們有來顯示所有的約會

的能力內

話,我想給他們一個特定貼切的「點擊」,並顯示它或編輯它

因爲我的應用程序與天路開始,在我的應用程序的索引我要跳到能力任命內幾天後,應用程序啓動(所以,而不是開始在/我的應用程序開始於/天例如)

App.IndexRoute = Ember.Route.extend({ 
    redirect: function() { 
     this.transitionTo('days'); 
    } 
}); 

模板看起來像這樣

application.handlebars(或者,如果你是內聯)

{{outlet}} 

days.handlebars

{{#each day in controller}} 
    {{#linkTo 'day' day}}view details about the day{{/linkTo}} 
{{/each}} 

{{outlet}} 

day.handlebars

{{#linkTo "appointments" target="controller"}}show all the apts{{/linkTo}} 

約會。車把

{{#each appointment in controller}} 
<a href="#" {{action 'addAppointment' appointment target="view"}}>{{appointment.start}} - {{appointment.end}}</a><br /> 
{{/each}} 

我的約會路線是這樣的(提供在上面的模板中的約會)

App.AppointmentsIndexRoute = Ember.Route.extend({ 
    model: function(params) { 
     var day = this.modelFor("day"); 
     return day.get('appointments'); 
    } 
});