Javascript 如何覆盖 Backbone.sync?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5096549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:39:07  来源:igfitidea点击:

How to override Backbone.sync?

javascriptbackbone.js

提问by picardo

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.js,我尝试的其中一件事是调用远程 API,因此我需要能够覆盖 Backbone.sync,正如我对文档的理解。

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 的谷歌组......有人可以指出一个这样做的例子吗?

回答by Raynos

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

看一下这个带注释的源示例,它们Backbone.sync用 localstorage 替代方案覆盖

backbone-localStorage

骨干本地存储

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

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

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

You need to fire either options.successor options.errordepending on whether the methodsucceeded. The methods are in the format:

您需要触发options.successoptions.error取决于是否method成功。方法的格式如下:

  • "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.
  • "create":期望您在服务器上创建模型
  • "read": 希望你从服务器读取这个模型并返回它
  • "update":期望您使用参数更新服务器上的模型
  • "delete": 希望您从服务器中删除模型。

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

你需要实现这 4 种方法并定义你想要的任何东西 "server"

Of course these are only the things that Backbone.syncmustimplement. You may implement more methodsand you may pass more paramaters back to successbut it's best not to do this.

当然,这些只是Backbone.sync必须执行的事情。您可以实现更多methods,也可以将更多参数传回,success但最好不要这样做。

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

最好确保它与Backbone.sync当前的功能相同,以便您对接口而不是实现进行编程。如果你想切换你的修改Backbone.sync,比如 localStorage 你不必自己扩展它来匹配你扩展的 Backbone.sync”

[Edit]

[编辑]

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

另请注意,您可以使用sync. 对每个引用Backbone.sync实际上都是(this.sync || Backbone.sync)如此,因此您只需执行以下操作:

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

    "sync": myOwnSpecificSync,

    ...
});

Backbone.syncis just the default global one that all models use unless the models have a syncmethod specifically set.

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

回答by Cyril N.

I know this answer is a bit too late, and the answer from @Raynos is great, but I did it a bit differently, and maybe it would be useful for you or for any other person trying to use an API with Backbone.

我知道这个答案有点太晚了,@Raynos 的答案很好,但我的做法有点不同,也许它对您或任何其他尝试将 API 与 Backbone 一起使用的人有用。

Instead of overriding Backbone.sync, I overrided Backbone.ajax, because it's where the ajax request is made.

我没有覆盖Backbone.sync,而是覆盖了Backbone.ajax,因为它是发出 ajax 请求的地方。

Here's an example :

这是一个例子:

// Set the default implementation of `Backbone.ajax` to proxy through to `$`.
Backbone.ajax = function() {
    var args = Array.prototype.slice.call(arguments, 0);

    // Here, I add the OAuth token (or any other token)
    // But before, I check that data exists, if not I add it
    if (args[0]['data'] === undefined) {
        args[0]['data'] = {};
    }
    args[0]['data']['token'] = 'any_api_token_here';

    return Backbone.$.ajax.apply(Backbone.$, args);
};

回答by Jesse Atkinson

I typically need to override backbone's syncmethod when I need to only sync certain attributes. A typical implementation looks like this:

sync当我只需要同步某些属性时,我通常需要覆盖主干的方法。典型的实现如下所示:

sync: function (method, model, options) {
  options.data = _.pick(this.attributes, 'foo', 'bar', 'baz');
  return Backbone.sync.call(this, method, model, options);
}