我正在尝试从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();
控制台输出显示mockjax收到了GET请求,但成功处理程序尚未被调用:
MOCK GET: /person/1
MOCK GET: /people
为什么我的成功处理程序没有被调用?
最佳答案
如果使用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
Here是jsfiddle。
关于javascript - backbone.js + jquery-mockjax : fetch success handler doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18900261/