【问题标题】:Backbone.js manually trigger "error" on a collectionBackbone.js 手动触发集合上的“错误”
【发布时间】: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 吗?

标签: javascript backbone.js


【解决方案1】:

我的博客上有一篇关于这类事情的帖子,不幸的是它是葡萄牙语的,但也许谷歌翻译可以帮助你。

http://www.rcarvalhojs.com/dicas/de/backbone/2014/06/24/gerenciando-o-estado-da-aplicacao.html

我喜欢这样处理

   GetSomething:->
  @result = @fetch(
                success:()=>
                  @trigger "succesthing1" if @result .status is 204
                  @trigger "successThing2" if @result .status is 200
                error:()=>
                  @trigger "errorThing" if @result .status is 401
)

现在我可以在视图中监听这些触发器,并针对来自服务器的特定结果采取正确的操作

【讨论】:

    【解决方案2】:

    目前我订阅Backbone sync,通过根据请求返回的承诺发送事件,见下面的例子:

    (function(Backbone) {
        var methods, _sync;
    
        _sync = Backbone.sync;
    
        methods = {
            beforeSend: function() {
                return this.trigger("sync:start", this);
            },
    
            error: function() {
                return this.trigger("sync:error", this);
            },
    
            complete: function() {
                return this.trigger("sync:stop", this);
            }
        };
    
        Backbone.sync = function(method, entity, options) {
            var sync;
    
            if (options == null) {
                options = {};
            }
    
            _.defaults(options, {
                beforeSend: _.bind(methods.beforeSend, entity),
                error: _.bind(methods.error, entity)
                complete: _.bind(methods.complete, entity)
            });
    
            sync = _sync(method, entity, options);
            if (!entity._fetch && method === "read") {
                return entity._fetch = sync;
            }
        };
    })(Backbone);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-07
      相关资源
      最近更新 更多