Ember.js RESTAdapter无法调用未定义的方法'then'

时间:2013-06-18 09:15:09

标签: ember.js handlebars.js ember-data

我有一个休息适配器:

App.Adapter = DS.RESTAdapter.extend({
    bulkCommit: true,
    url: 'http://10.0.0.106/bank',
    ajax: function (url, type, hash) {
        hash.url = url;
        hash.type = type;
        hash.context = this;
        hash.headers = {
            'Session-Token': '65f1b8925f8eb2780730a4a2993693699e9a98dad5d2995452c24758d2c59706'
        };

        if (hash.data && type !== 'GET') {
            hash.data = JSON.stringify(hash.data);
        }

        $.ajax(hash);
    }
});

App.Store = DS.Store.extend({
    revision: 13,
    adapter: App.Adapter
});

路线:

App.UsersRoute = Ember.Route.extend({
    model: function() {
        return App.User.find();
    }
});

模特:

App.User = DS.Model.extend({
    firstname: DS.attr("string"),
    lastname: DS.attr("string"),
    email: DS.attr("string"),
    user_id: DS.attr("string"),
    role: DS.attr("string"),
    phone: DS.attr("string")
});

请求运作良好,并告诉我:

{
    "error": null,
    "payload": {
        "bank_users": [{
                "firstname": "Ugo",
                "lastname": "Doe",
                "email": "ugo@doe.com",
                "user_id": 1,
                "role": "BANK_USER",
                "phone": null
            }, {
                "firstname": "Ugo Clone",
                "lastname": "Doe",
                "email": "ugo-clone@doe.com",
                "user_id": 2,
                "role": "BANK_USER",
                "phone": null
            }
        ]
    }
}

在我的模板中;我就是这样:

<h2>this is users !!</h2>

{{#each model}}
    {{firstname}}
{{/each}}

错误是:Uncaught TypeError: Cannot call method 'then' of undefined

这发生在ember-data中,这里:

  findAll: function(store, type, since) {
  var root, adapter;

  root = this.rootForType(type);
  adapter = this;

  return this.ajax(this.buildURL(root), "GET",{
    data: this.sinceQuery(since)
  }).then(function(json) {
    adapter.didFindAll(store, type, json);
  }).then(null, rejectionHandler);
}

我看来since未定义;我做错了什么? :)

1 个答案:

答案 0 :(得分:1)

我猜,因为你要覆盖RESTAdapter ajax方法应该是最后一行

$.ajax(hash);

不是吗?

return $.ajax(hash);

希望它有所帮助。