2

我有一个 ember.js 原型,到目前为止它运行良好。自从我上次部署以来,我一直收到错误 Uncaught TypeError: Cannot read property 'hash' of undefined。

app.js 文件

// Application
Welcome = Ember.Application.create({
    ready : function() {
    }
});

// Model
Welcome.Ticket = Ember.Object.extend({
    id : null,
    buyerPartyId : null,
    name : null,
    priority : null,
    description : null,
    comment : null
}); 

// Controller
Welcome.ticketsController = Ember.ArrayController.create({
    content : [],

    loadTickets : function() {
        var self = this;
        $.getJSON('url here', function(
                data) {
            for ( var i = 0; i < data.serviceRequest.length; i++) {
                self.pushObject(Welcome.Ticket.create(data.serviceRequest[i]));
            }
        });
    }
});

索引.html

<!doctype html>
<html>
<head>
<title>Ticket List</title>
<link rel="stylesheet" href="css/normalize.css">
<script
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/libs/ember-0.9.5.min.js"></script>
<script src="js/app.js"></script>
</head>
<body>
    <h1>Ticket List</h1>
    <script type="text/x-handlebars">
    {{#view Ember.Button target="Welcome.ticketsController" action="loadTickets"}}
        Load Tickets
    {{/view}}

    {{#each contentBinding="Welcome.ticketsController" tagName="ul"}}
        <b>{{content.id}}</b> - {{content.buyerPartyId}}, <i>{{content.name}}</i>
    {{/each}}
</script>
</body>
</html>

我确信我忽略了一些简单的东西!任何建议,将不胜感激 !!

这是来自 Chrome 开发控制台的错误堆栈。

Uncaught TypeError: Cannot read property 'hash' of undefined ember-0.9.5.min.js:13
(anonymous function) ember-0.9.5.min.js:13
(anonymous function)
b.VM.template ember-0.9.5.min.js:9
Ember.View.Ember.Object.extend.render ember-0.9.5.min.js:12
Ember.View.Ember.Object.extend.renderToBuffer ember-0.9.5.min.js:12
Ember.View.Ember.Object.extend.createElement ember-0.9.5.min.js:12
Ember.View.states.preRender.insertElement ember-0.9.5.min.js:12
Ember.View.Ember.Object.extend.invokeForState ember-0.9.5.min.js:12
c ember-0.9.5.min.js:10
j ember-0.9.5.min.js:10
f.flush ember-0.9.5.min.js:10
f.end ember-0.9.5.min.js:10
Ember.run.end ember-0.9.5.min.js:10
i ember-0.9.5.min.js:10
4

1 回答 1

2

AFAIK,{{each}}您需要直接提供控制器。就像是:

{{#each Welcome.ticketController}}
{{/each}}

看看这个 jsFiddle http://jsfiddle.net/lifeinafolder/nVr4r/。它不再抛出错误。

“加载门票”按钮将无法使用,因为您还没有网址。

Emberjs documentation has a couple of examples using {{each}} too. You should take a look.

Also, you might wanna update your emberjs. In the ember world, 0.9.5.min is pretty old :)

于 2012-09-01T08:46:36.750 回答