2012-07-25 64 views
0

有一个简单的ember.js应用程序,其中一个视图显示在网页的特定位置。看看这个的jsfiddle:http://jsfiddle.net/jkkK3/9/Ember模板显示两次

App = Ember.Application.create({ 

    ready: function(){ 
     this._super(); 
     this.ApplicationView.create().appendTo(".content"); 
    }, 

    ApplicationController: Ember.Controller.extend({ 
    }), 

    ApplicationView: Ember.View.extend({ 
    templateName: 'application' 
    }), 

    Router: Ember.Router.extend({ 
    root: Ember.Route.extend({ 
    }) 
    }) 
}); 

我的问题是:为什么“这里的一些内容”的元素显示两次?它在我移除路由器时起作用,但这正是我无法做到的,因为我尝试将路由器添加到我的Ember应用程序中。您能否帮我在红色框内显示一次应用程序视图?

回答

3

使用路由器时,默认情况下使用applicationController/view。在你准备好的方法中,你明确地附加它。所以'申请'模板被附加两次。删除附加它准备好的方法,它只会被附加一次。

默认情况下它是附加在身上,但如果你想覆盖Ember.Application

Ember.Application.create({ 
    rootElement : '.content', 
    .... 
}) 
+0

的使用rootElement的财产是的,你是对的。那么我怎么才能显示它一次,将它追加到“.content”div? – 2012-07-25 16:08:47

+0

你不需要在ready()方法中追加它 – zaplitny 2012-07-25 16:10:55

+0

然后它被放置在body元素中。我需要把它放在特定的元素中,在我的例子中是“.content”。 – 2012-07-25 16:11:50