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

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

问题描述

我的后端有两个独立的页,一个用于处理模型保存请求,另一个用于模型取。

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

什么是调用save())来使用不同的网址,最好的方法,并获取(?谢谢你。

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

编辑:
研究注释的源代码后,我看到一个能够真正提供一个选项哈希的获取保存

//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(),它总是被正确地散列我的模型的属性。但现在,我想提供一个保存URL',我忍不住要称

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 参数中。

推荐答案

如果你读,你可能已经有了一个可行的解决方案的来源。你基本上有两个选择(甚至更多) -

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


  1. 通网址中保存()/读取()

保存()有两个参数ATTR和选项
 ATTR - 是模型属性的哈希值,用于更新模型保存之前。
例如。

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然后$阿贾克斯) - 设置URL在这个哈希会正常运行。
你可以通过虚假的,不确定的,或者{}来跳过更新的第一个参数。

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

而不是有网址的每次调用保存()或fetch()写自己的同步功能来计算的网址为您的时间分散到您的code,委托原来的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

如。 (此同步功能增加/保存在创建,更新和删除操作的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的使用不同的网址模型保存和取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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