1

I'm trying to fetch a single model and a collection of models from mockjax (jsfiddle):

var Person = Backbone.Model.extend({ urlRoot: "/person" }), 
    person,
    People = Backbone.Collection.extend({ url: "/people", model: Person }), 
    people;

// Fetching a single model
$.mockjax({
    url: "/person/*",
    responseText: { id: 1, name: "Ann", age: 10 }
});
person = new Person({ id: 1 });
person.fetch({
    success: function () {
        console.log(person.get("name"));
    }
});
$.mockjaxClear();

// Fetching a collection of models
$.mockjax({
    url: '/people',
    responseText: [
        { id: 1, name: "Ann", age: 10 }, 
        { id: 2, name: "Bill", age: 20 }
    ]
});
var people = new People();
people.fetch({
    success: function () {
        console.log(people.length);
    }
});
$.mockjaxClear();

The console output shows that mockjax received the GET request, but the success handlers haven't been called:

MOCK GET: /person/1 
MOCK GET: /people

Why are my success handlers not called?

4

1 回答 1

1

如果使用 mockjax 版本 1.5.1,则不会调用处理程序。版本 1.5.2 可以:

MOCK GET: /person/1 > Object {url: "/person/1", type: "GET", isLocal: true, global: true, processData: true…}
MOCK GET: /people > Object {url: "/people", type: "GET", isLocal: true, global: true, processData: true…}
Ann
2 

是jsfiddle。

于 2013-09-19T16:57:33.103 回答