Ember无法在模型上找到find()方法

时间:2012-08-21 18:03:19

标签: javascript ember.js

Ember似乎无法找到我在我的Property模型上实现的findAll()find()方法。以下是我得到的错误:

TypeError: App.Property.findAll is not a function

Error: assertion failed: Expected App.Property to implement `find` for use in 'root.property' `deserialize`. Please implement the `find` method or overwrite `deserialize`.

我的路由器设置如下:

App.Router = Ember.Router.extend({
    showProperty: Ember.Route.transitionTo('property'),
    root: Ember.Route.extend({
        home: Ember.Route.extend({
            route: '/',
            connectOutlets: function(router) {
                router.get('applicationController').connectOutlet('home', App.Property.findAll());
            }
        }),
        property: Ember.Route.extend({
            route: '/property/:property_id',
            connectOutlets: function(router, property) {
                router.get('applicationController').connectOutlet('property', property);
            },
        }),
    })
});

这是我的模特:

App.Property = Ember.Object.extend({
    id: null,
    address: null,
    address_2: null,
    city: null,
    state: null,
    zip_code: null,
    created_at: new Date(0),
    updated_at: new Date(0),
    find: function() {
        // ...
    },
    findAll: function() {
        // ...
    }
});

我做错了什么?这些方法是应该使用Property模型还是应该去其他地方?我应该覆盖deserialize()方法而不是使用find()吗?但即使我使用该解决方法findAll()仍然无效,我仍然会得到第一个错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:8)

findfindAll方法应在reopenClass中声明,而不是在extend中声明,因为您要定义类方法,而不是实例方法。 例如:

App.Property = Ember.Object.extend({
    id: null,
    address: null,
    address_2: null,
    city: null,
    state: null,
    zip_code: null,
    created_at: new Date(0),
    updated_at: new Date(0)
});

App.Property.reopenClass({
    find: function() {
        // ...
    },
    findAll: function() {
        // ...
    }
});