【问题标题】:Why doesn't application.hbs render in emberjs为什么 application.hbs 不在 emberjs 中呈现
【发布时间】:2013-01-19 16:15:51
【问题描述】:

使用 ember 1.0.0-pre3

我有一个带有此代码的小应用程序:

window.App = Ember.Application.create()

App.ApplicationController = Ember.Controller.extend({})

App.Router.reopen
  location: 'history'

App.Router.map ->
  @resource 'users', ->
    @route 'new'

App.IndexRoute = Ember.Route.extend
  renderTemplate: ->
    @render('index')

这是模板目录中的application.hbs:

<div class='navbar navbar-inverse navbar-fixed-top'>
  <div class='navbar-inner'>
    <div class='container'>
      <div class='nav-collapse collapse'>
        <ul class='nav'>
          <li>{{#linkTo 'index'}}Home{{/linkTo}}</li>
          <li>{{#linkTo 'users.index'}}Users{{/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>

问题是它不渲染这个模板。在基本 URL“http://127.0.0.1:3000/”处加载应用程序时不会引发错误。如果我尝试未定义的路由,它会抛出错误,所以我知道 Ember 已加载。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    您的 ember 代码似乎没有任何问题。我在 jsbin 上做了一个副本,效果很好:http://jsbin.com/ipivoz/1/edit

    很确定这意味着您的模板没有被编译。可以肯定的是,尝试将以下内容添加到您的应用程序就绪挂钩中:

    window.App = Ember.Application.create({
      ready: function() {
        console.log("Ember.TEMPLATES: ", Ember.TEMPLATES);
      }
    });
    

    Ember 期望已编译的车把模板位于此数组中。如果您在打开 js 控制台的情况下运行上述 jsbin,您会看到数组中有一个模板“应用程序”。我的猜测是,如果您在您的环境中尝试相同的操作,该数组将为空。

    有很多方法可以做到这一点,这取决于您的环境。由于您在端口 3000 上运行,我猜那是导轨。在这种情况下,请查看 ember-rails gem。到目前为止,最简单的短期方法是在 HTML 页面中使用 script 标记定义模板,就像我在 jsbin 中所做的那样。

    【讨论】:

    • 是的,ember 模板对象是空的。我正在尝试使用 handlebars_assets gem。
    • 结束了 rake assets:precompile。不知道为什么 sprockets 没有在开发中即时编译它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-18
    相关资源
    最近更新 更多