Ember.js“无法找到模板”错误

时间:2012-08-03 02:12:54

标签: templates exception ember.js

当我启动DOM时,我收到“无法找到模板”错误。这是我的代码:

//javascripts/app/app.js
MyApp = Ember.Application.create();

MyApp.store = DS.Store.create({
  revision: 4,
  adapter: DS.RESTAdapter.create({ bulkCommit: false })
});

//javascripts/app/models/event.js
MyApp.Event = DS.Model.extend({
    id: DS.attr('number'),
    league_id: DS.attr('number')
});

//javascripts/app/controllers/event.js
MyApp.eventsController = Ember.ArrayController.create({
      content: [],

      init: function() {
        this.content = jQuery.get('events.json');
      }
});

//javascripts/app/views/events/list.js
MyApp.EventListView = Ember.View.extend({
  templateName: 'app/templates/events/list',
  eventsBinding: 'MyApp.eventsController'
});

//javascripts/app/templates/events/list.handlebars
{{#each events}}
    {{view MyApp.EventListView eventsBinding='this'}}
{{/each}}

//app/views/events/index.html.erb
<script type='text/x-handlebars'>
  {{ view MyApp.EventListView }}
</script>

我尝试了this Q&A中建议的其中一个修复程序,但无法让它为我工作。我做错了什么?

1 个答案:

答案 0 :(得分:2)

对于您的错误,如果整个代码在此处,您错过了把手<script>模板中的list标记:

<!-- conforms to MyApp.EventListView templateName property -->
<script type='text/x-handlebars' data-template-name="list">
   {{#each events}}
<!-- you dont have to insert {{view MyApp.EventListView eventsBinding='this'}} 
here, as I think you want to manipulate an event -->
       {{league_id}} 
   {/each}}
</script>

顺便说一下,你的控制器的init方法是错误的:

MyApp.eventsController = Ember.ArrayController.create({
  content: [],

  init: function() {
    this._super(); // when you override init(),
                   // you have to call the super class init().

    this.set('content', jQuery.get('events.json')); 
    // that the way to set properties with ember. You have to use this.
  }
});

如果没有帮助,正如@Rajat建议的那样,请发布完整代码的jsfiddle。