Backbone.js 使用不同的 url 进行模型保存和获取 [英] backbone.js use different urls for model save and fetch

查看:9
本文介绍了Backbone.js 使用不同的 url 进行模型保存和获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的后端有两个单独的页面,一个用于处理模型保存请求,另一个用于模型获取.

My back-end has two separate pages, one for handling the model save request and the other for model fetch.

调用 save() 和 fetch() 以使用不同 URL 的最佳方法是什么?谢谢.

What is the best approach for calling save() and fetch() to use different URLs? Thanks.

在研究了带注释的源代码后,我发现实际上可以将 options 哈希值提供给 获取保存

After studying the annotated source, I see that one can actually supply an options hash to fetch and save

//taken from backbone source:
save : function(attrs, options) {
  options || (options = {});
  if (attrs && !this.set(attrs, options)) return false;
  var model = this;
  var success = options.success;
  options.success = function(resp, status, xhr) {
    if (!model.set(model.parse(resp, xhr), options)) return false;
    if (success) success(model, resp, xhr);
  };
  options.error = wrapError(options.error, model, options);
  var method = this.isNew() ? 'create' : 'update';
  return (this.sync || Backbone.sync).call(this, method, this, options);
},

在存档中,attrs 有什么作用?我只是在没有传入任何内容的情况下调用 myModel.save() 并且它总是正确地散列我的模型的属性.但现在我想提供一个保存网址",我很想打电话

In the save, what purpose does the attrs serve? I've just been calling myModel.save() without passing anything in and it's always been hashing my model's attributes correctly. But now that I want to supply a 'save url' and I'm tempted to call

myModel.save(undefined, {
    url: 'myPath'
})

跳过"第一个 attrs 参数所需的 undefined.

with the undefined required to 'skip' the first attrs paramter.

推荐答案

如果您正在阅读源代码,您可能已经有了一个可行的解决方案.您基本上有两个选择(可能更多)-

If you're reading the source you probably already have a working solution. You essentially have two options (probably more)-

  1. 在 save()/fetch() 中传递 URL

save() 需要两个参数 attr 和 optionsattr - 是模型属性的散列,用于在保存前更新模型.例如.

save() takes two parameters attr and options attr - is a hash of model attributes and is used to update the model before save. eg.

myModel.save(attrs)

相当于

myModel.set(attrs)
myModel.save()

第二个参数是一个选项散列,它被传递给 this.sync()(然后是 Backbone.sync 和 $.ajax)——在这个散列中设置 url 将按预期工作.您可以将 false、undefined 或 {} 作为第一个参数传递以跳过更新.

The second parameter is an options hash, which is passed down to this.sync() (and then Backbone.sync and then $.ajax) - setting the url in this hash will work as expected. You can pass false, undefined, or {} as the first parameter to skip the update.

  1. 覆盖 Backbone.sync

不要在每次调用 save() 或 fetch() 时将 url 分散在您的代码中,而是编写您自己的同步函数来为您计算 url,委托给原始 Backbone.sync 来完成繁重的工作

Rather than have url's scattered throughout your code every time you call save() or fetch() write your own sync function to compute the url for you, the delegate to the original Backbone.sync to do the heavy lifting

例如.(此同步功能将/save 添加到 CREATE、UPDATE 和 DELETE 操作的 url)

eg. (This sync function adds /save to the url on CREATE, UPDATE and DELETE actions)

function mySyncFunction(method, model, options){
  if(method=='GET'){
    options.url = model.url; 
  }else{
     options.url = model.url + '/save'; 
  }
  return Backbone.sync(method, model, options);
}

要使用自定义同步方法,只需将其声明为模型的一部分

To use your custom sync method just declare it as part of your model

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

  "sync": mySyncFunction,

  ...
});

这篇关于Backbone.js 使用不同的 url 进行模型保存和获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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