【发布时间】:2014-07-27 07:53:43
【问题描述】:
如果服务器/json 返回特定的状态(例如,没有结果),我希望我的收集失败。
问题:默认的error-handler没有被调用(因为collection成功获取了json。所以我的想法是使用parse函数在json中查找error-code。
但是如何触发错误方法并通知我的视图(并停止收集尝试创建模型)
/*global define*/
define([
'underscore',
'backbone',
'models/mymodel'
], function (_, Backbone, MyModel) {
'use strict';
var SomeCollection = Backbone.Collection.extend({
model: MyModel,
value: null,
url: function() {
return url: "data/list.json";
},
initialize: function(models, options) {
this.zipcode = options.zipcode;
},
parse: function(response, xhr) {
if(response.status == "OK") {
console.info("Status: "+response.status);
return response.results;
} else {
console.warn("Status: "+response.status+" – Message: "+response.message);
this.trigger('fail') // does not work
return response;
}
}
});
return SomeCollection;
});
【问题讨论】:
-
你必须在你的视图中监听集合实例上的事件
-
我愿意。但是我没有报错(因为json加载成功)
-
你想抛出什么样的错误?显示一些错误处理程序代码?你在使用 Backbone.Events 吗?