0

我最近开始在我的项目中实现 ember 数据库,但现在一切都坏了,google chrome 控制台为我提供了这些错误:

- Error while loading route: TypeError {} 
- Uncaught TypeError: Cannot read property 'id' of undefined 
- Port: Could not establish connection. Receiving end does not exist.

我要打开的 URL 是:/#/track/85

这是相关代码:

Shoutzor = Ember.Application.create();
Shoutzor.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: '/api/emberstore',
    host: 'http://www.shoutzor.nl'
});

var attr = DS.attr,
    hasMany = DS.hasMany,
    belongsTo = DS.belongsTo;

Shoutzor.Track = DS.Model.extend({
    id: attr('number'),
    title: attr('string'),
    //length: attr('number'),
    //artist: hasMany('artist'),
    //album: hasMany('album'),

    /* Convert the length in seconds to a string like '01:55' */
    convertedLength: function() {
        var sec_num = parseInt(this.get('length'), 10); // don't forget the second parm
        var hours   = Math.floor(sec_num / 3600);
        var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
        var seconds = sec_num - (hours * 3600) - (minutes * 60);

        if (hours   < 10 && hours > 0) {hours   = "0"+hours;}
        if (minutes < 10 && minutes > 0) {minutes = "0"+minutes;}
        if (seconds < 10) {seconds = "0"+seconds;}
        var time    = ((hours != 0) ? hours + ':' : '') + ((minutes != 0) ? minutes +':' : '') + seconds;

        return time;
    }.property('length')
});

Shoutzor.Album = DS.Model.extend({
    id: attr('number'),
    artist: belongsTo('artist'),
    title: attr('string'),
    cover: attr('string')
});

Shoutzor.Artist = DS.Model.extend({
    id: attr('number'),
    name: attr('string'),
    profileimage: attr('string')
});

Shoutzor.Router.map(function() {
    //Track Page
    this.route("track", { path: "/track/:id" });
});

Shoutzor.TrackRoute = Ember.Route.extend({
    setupController: function(controller) {
        controller.set('pageTitle', "Track");
    },

    renderTemplate: function() {
        this.render('TrackContent', { outlet: 'pageContent', into: 'application' });
    },

    model: function(params) {
        return this.store.find('track', params.id);
    }
});

Shoutzor.TrackController = Ember.Controller.extend();

Shoutzor.TrackView = Ember.View.extend();

我的 API 提供如下响应:

{"tracks":{"id":85,"title":"Untitled","file":{"filename":"Lines of Latitude - You want it (Free Download).mp3","filepath":"\/home\/vhosts\/domains\/shoutzor\/music\/Lines of Latitude - You want it (Free Download).mp3","crc":"51ca8346","length":262,"id3":[]},"artist":[],"album":[]}}

我一直在查看多个 SO 帖子和谷歌搜索结果,但没有一个能解决我的问题(或者我正在寻找错误的东西),

任何帮助是极大的赞赏!

4

1 回答 1

2

您不需要包含id: DS.attr()在模型类中。此外,单一资源的响应应该有根键track而不是tracks

{"track":{"id":85,"title":"Untitled","file":{"filename":"Lines of Latitude - You want it (Free Download).mp3","filepath":"\/home\/vhosts\/domains\/shoutzor\/music\/Lines of Latitude - You want it (Free Download).mp3","crc":"51ca8346","length":262,"id3":[]},"artist":[],"album":[]}}`
于 2013-10-17T14:57:26.877 回答