如何使mockjax返回一个ember-data对象

时间:2014-10-16 18:54:57

标签: ember.js ember-data mockjax

鉴于我有Ember数据模型(这是CoffeeScript):

Person = DS.Model.extend firstName: DS.attr("string") lastName: DS.attr("string")

或作为JavaScript:

Person = DS.Model.extend({ firstName: DS.attr("string"), lastName: DS.attr("string") });

如何使用mockjax从商店返回Person对象?这个mockjax不起作用(我认为),因为它返回一个匿名JavaScript对象,而不是Person对象。

$.mockjax type: "GET" url: "/people" data: { firstName: "John"} status: "200" dataType: "json" response: (d) -> person = { id: 2 firstName: John lastName: Smith } @responseText = person

或作为JavaScript:

$.mockjax({ type: "GET", url: "/people", data: { firstName: "John" }, status: "200", dataType: "json", response: function(d) { var person; person = { id: 2, firstName: John, lastName: Smith }; return this.responseText = person; } });

我正在使用ES6 FYI。

1 个答案:

答案 0 :(得分:0)

你很接近,但不是将变量传递给responseText,而是将它传递给你正在创建的整个对象。

像这样:

$.mockjax
    type: "GET"
    url: "/people"
    data: { firstName: "John"}
    status: "200"
    dataType: "json"
    response: (d) ->
     @responseText = person: 
      [
       {
         id: 2
         firstName: John
         lastName: Smith
       }
      ]