如何覆盖 Backbone.sync? [英] How to override Backbone.sync?

查看:15
本文介绍了如何覆盖 Backbone.sync?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Backbone.js,我尝试的其中一件事是调用远程 API,因此我需要能够覆盖 Backbone.sync,据我了解 文档.

I'm trying out Backbone.js, and one of the things I'm trying is to make a call to a remote API, so I need to be able to override Backbone.sync, as I understand the documentation.

文档本身中没有说明如何执行此操作的示例,而且似乎没有 Backbone 的 google 组……有人可以指出这样做的示例吗?

There isn't an example of how to do that in the documentation itself, and there doesn't appear to be a google group for Backbone... can someone point out an example for doing this?

推荐答案

看看这个带注释的源代码示例,他们用本地存储替代方案覆盖 Backbone.sync

Take a look at this annotated source example where they overwrite Backbone.sync with a localstorage alternative

backbone-localStorage

基本上 Backbone.sync 应该是一个带有 4 个参数的函数:

Basically Backbone.sync should be a function that takes 4 arguments:

Backbone.sync = function(method, model, options) { };

您需要根据 method 是否成功触发 options.successoptions.error.方法的格式如下:

You need to fire either options.success or options.error depending on whether the method succeeded. The methods are in the format:

  • "create" : 希望您在服务器上创建模型
  • "read" : 希望你从服务器读取这个模型并返回它
  • "update" :期望您使用参数更新服务器上的模型
  • "delete" :希望您从服务器中删除模型.
  • "create" : expected that you create the model on the server
  • "read" : expected that you read this model from the server and return it
  • "update" : expected that you update the model on the server with the argument
  • "delete" : expected that you delete the model from the server.

你需要实现这 4 个方法并为你的 "server"

You need to implement those 4 methods and define whatever you want for your "server"

当然,这些只是Backbone.sync 必须实现的东西.你可以实现更多的方法,你可以将更多的参数传回success,但最好不要这样做.

Of course these are only the things that Backbone.sync must implement. You may implement more methods and you may pass more paramaters back to success but it's best not to do this.

最好确保它与 Backbone.sync 当前所做的相同,以便您对接口而不是实现进行编程.如果你想把你修改过的 Backbone.sync 换成 localStorage 你就不必自己扩展它来匹配你扩展的 Backbone.sync"

It's best to make sure it does the same as Backbone.sync does currently so that your programming to an interface rather then an implementation. If you want to switch out your modified Backbone.sync for say the localStorage one you won't have to extend it yourself to match your extended Backbone.sync"

另请注意,您可以使用多个 sync 实现.每个对 Backbone.sync 的引用实际上都是 (this.sync || Backbone.sync) 所以你只需要做一些类似的事情:

Also do note that you can use multiple implementations of sync. Every reference to Backbone.sync is actaully (this.sync || Backbone.sync) so you just have to do something like:

var MyModel = Backbone.Model.extend({ 
    ...

    "sync": myOwnSpecificSync,

    ...
});

Backbone.sync 只是所有模型都使用的默认全局变量,除非模型专门设置了 sync 方法.

Backbone.sync is just the default global one that all models use unless the models have a sync method specifically set.

这篇关于如何覆盖 Backbone.sync?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆