“如何"在 Backbone.js 中保存整个集合 - Backbone.sync 或 jQuery.ajax? [英] "How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

查看:15
本文介绍了“如何"在 Backbone.js 中保存整个集合 - Backbone.sync 或 jQuery.ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很清楚这是可以做到的,而且我看过很多地方(包括:保存整个集合的最佳实践?).但我仍然不清楚它究竟如何"用代码编写?(这篇文章用英文解释了它.有一个 javascript 特定的解释会很棒:)

I am well aware it can be done and I've looked at quite a few places (including: Best practice for saving an entire collection?). But I'm still not clear "exactly how" is it written in code? (the post explains it in English. It'd be great to have a javascript specific explanation :)

假设我有一个模型集合 - 模型本身可能有嵌套集合.我已经覆盖了父集合的 toJSON() 方法,并且我得到了一个有效的 JSON 对象.我希望保存"整个集合(对应的 JSON),但主干似乎没有内置该功能.

Say I have a collection of models - the models themselves may have nested collections. I have overridden the toJSON() method of the parent collection and I am getting a valid JSON object. I wish to "save" the entire collection (corresponding JSON), but backbone doesn't seem to come in-built with that functionality.

var MyCollection = Backbone.Collection.extend({
model:MyModel,

//something to save?
save: function() {
   //what to write here?
 }

});

我知道你有什么要说的:

I know somewhere you have to say:

Backbone.sync = function(method, model, options){
/*
 * What goes in here?? If at all anything needs to be done?
 * Where to declare this in the program? And how is it called?
 */
}

一旦视图"完成处理,它负责告诉集合将自身保存"在服务器上(能够处理批量更新/创建请求).

Once the 'view' is done with the processing it is responsible for telling the collection to "save" itself on the server (capable of handling a bulk update/create request).

出现的问题:

  1. 如何/在代码中编写什么来将它们连接在一起"?
  2. 回调的正确"位置是什么以及如何指定成功/错误"回调?我的意思是在语法上?我不清楚在主干中注册回调的语法...

如果这确实是一项棘手的工作,那么我们可以在视图中调用 jQuery.ajax 并将 this.successMethodthis.errorMethod 作为成功/错误回调传递吗??它会起作用吗?

If it is indeed a tricky job then can we call jQuery.ajax within a view and pass the this.successMethod or this.errorMethod as success/error callbacks?? Will it work?

我需要与主干的思维方式保持同步 - 我知道我肯定错过了一些东西,同步整个集合.

I need to get in sync with backbone's way of thinking - I know I'm definitely missing something w.r.t., syncing of entire collections.

推荐答案

我的直接想法是不要覆盖 Backbone.Collection 上 save 方法的方法,而是将集合包装在另一个 Backbone.Model 中并覆盖其上的 toJSON 方法.然后 Backbone.js 会将模型视为单一资源,您不必像 backone 那样想太多.

My immediate thought is not to override the method on save method on Backbone.Collection but wrap the collection in another Backbone.Model and override the toJSON method on that. Then Backbone.js will treat the model as a single resource and you don't have to hack the way backone thinks too much.

请注意,Backbone.Collection 有一个 toJSON 方法,因此您的大部分工作都已为您完成.您只需将包装器 Backbone.Model 的 toJSON 方法代理到 Backbone.collection.

Note that Backbone.Collection has a toJSON method so most of your work is done for you. You just have to proxy the toJSON method of your wrapper Backbone.Model to the Backbone.collection.

var MyCollectionWrapper = Backbone.Model.extend({
url: "/bulkupload",

//something to save?
toJSON: function() {
    return this.model.toJSON(); // where model is the collection class YOU defined above
 }

});

这篇关于“如何"在 Backbone.js 中保存整个集合 - Backbone.sync 或 jQuery.ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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