简体   繁体   English

Ember.js出口无法正常工作

[英]Ember.js outlet not working properly

I'm currently learning ember.js and I'm stuck with a very basic problem: I can't find a way to render a template inside another one using outlets. 我目前正在学习ember.js,但遇到了一个非常基本的问题:我找不到使用插座在另一个模板中渲染模板的方法。 These are the files I have: 这些是我拥有的文件:

application.handlebar application.handlebar

<div class='navbar navbar-inverse navbar-fixed-top' role='navigation'>
    <div class='navbar-inner'>
        <div class='container'>
            <a class="brand" href="#">App</a>
            <div class='nav-collapse collapse'>
                <ul class='nav'>
                    <li>{{#linkTo 'application'}}Home{{/linkTo}}</li>
                    <li>{{#linkTo 'posts'}}Posts{{/linkTo}}</li>
                </ul>
            </div>
        </div>
    </div>
</div>
<div class='container' id='main'>
    <div class='content'>
        <div class='row'>
            <div class='span12'>
                <div class='page-header'></div>
                {{outlet}}
            </div>
        </div>
    </div>
</div>
</div>

posts.handlebars: posts.handlebars:

    <ul>
{{#each controller}}
  <li>
  {{title}}
  </li>
{{/each}}
</ul>

posts_routes: posts_routes:

App.PostsRoute = Ember.Route.extend({
    model: function() {

       return App.Post.find();
    }
});

router.js: router.js:

App.Router.map(function() {
    this.route('posts',{ path: '/posts' }); 
    this.route('application',{ path: '/' }); 
});

Here is what happens: when I load the application, the page is currently displayed with the nav bar and an empty white page. 这是发生的情况:加载应用程序时,当前显示该页面的导航栏和一个空白的页面。 Clicking the "Posts" link shows the posts template, but the nav disappear as if the posts template replaced the whole "application" template. 单击“帖子”链接将显示帖子模板,但是导航消失,就好像帖子模板替换了整个“应用程序”模板一样。 Can you help me figure out why this happens? 您能帮我弄清楚为什么会这样吗?

Internally ember use an ApplicationRoute , I think that using this.route('application',{ path: '/' }); 内部余烬使用ApplicationRoute ,我认为使用this.route('application',{ path: '/' }); is causing conflict. 造成冲突。

So just remove the this.route('application',{ path: '/' }); 因此,只需删除this.route('application',{ path: '/' }); and all will work. 一切都会工作。

If you need a home/initial page you can create a index.handlebars template, because ember automatically create a this.route('index',{ path: '/' }); 如果需要主页/初始页,则可以创建index.handlebars模板,因为ember会自动创建this.route('index',{ path: '/' }); . And change linkTo 'application' to linkTo 'index' , to get the expected behavior. 并将linkTo 'application'更改为linkTo 'index' ,以获取预期的行为。

Please give a look in that fiddle to see this working http://jsfiddle.net/marciojunior/HpmwB/ 请在该小提琴中看一下,以查看此工作原理http://jsfiddle.net/marciojunior/HpmwB/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM